Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Donovan Rucastle · Feb 24, 2013 at 07:48 AM · c#prefabarray

How to load prefabs into array

Hi guys

I was just wondering how I would load an array of prefabs using the resources folder ? Resources.load

I want to do this in C#

I know I would porbably use. public GameObject[] item; But how would I load the resources into an array and name them from item0 to how ever many items I have ? Eg. Item0, item1, item2,

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image robertbu · Feb 24, 2013 at 03:53 PM 0
Share

Note you don't have to use Resources.Load(). You can put them anywhere in Assets and then drag and drop them on the array positions in the inspector.

2 Replies

· Add your reply
  • Sort: 
avatar image
9
Best Answer

Answer by Henrik Poulsen · Feb 24, 2013 at 10:44 AM

There's a couple of ways to do this. The easiest would most likely be:

 public GameObject[] items;
 void Start()
 {
     // If you store all your items that you want to load in the same folder (Assets/Resources/MyItemsToLoad).
     items = Resources.LoadAll("MyItemsToLoad") as GameObject[];
 }

Then it would load all files in that folder. If you keep it to just the items that you want to load and nothing else then it would work nicely.

But if you don't want to do it like this you could do a looping solution instead.

 using System.Collections.Generic;    
 public List<GameObject> items = new List<GameObject>();
 
 void Start()
 {
     GameObject obj = null;
     int counter = 0;
     bool done = false;
     while(!done)
     {
         // We just keep loading until obj becomes null
         obj = Resources.Load("Item" + counter) as GameObject;
         if(obj == null)
             done = true; // Let's stop this now.
         else
             items.Add(obj);
         ++counter;
     }
 }

I would prefer the first solution, since I don't like to rely on errors (couldn't load) to know when you are done, but that's a matter of taste and what best suits your situation.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image W4rf4c3 · Jun 08, 2018 at 01:40 PM 0
Share

Is the Resources.Load recursive? I have other GameObjects inside child folders that I want the script to add too.

avatar image
0

Answer by BootySmashDeluxe · May 05 at 09:10 PM

Here is an example of code that did work for me.

 private GameObject[] clouds;
 clouds = Resources.LoadAll<GameObject>("Prefabs/Clouds");

Just make sure you have the gameobjects in Assets/Resources/foldername ie Assets/Resources/Prefabs/Clouds

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

15 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

OverlapSphere for parallel arrays 1 Answer

Multiple Cars not working 1 Answer

Instantiating an array of objects - how can I instantiate a certain prefab only once? 1 Answer

Array only returning first value C# 1 Answer

Prefabs instantiated from an array are keeping their public int value 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges