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
21
Question by glasstongue · May 21, 2014 at 03:54 AM · instantiateprefabsarrayslists

Adding prefabs to a list or an array from a folder and instantiating them.

Thanks to a few of the awesome members of Answers I managed to populate an array with prefab objects and instantiate them in my scene (from a folder in in my project) - then figured out I shouldn't use an array and figured out how to do it in a list.

Sharing it here because this information was so hard to find despite it being pretty simple to implement (plus it helps me solidify how it works in my mind.)


How to populate an Array with Prefabs from a folder (If you need to remove objects from your collection of prefabs use a list):

     //define an Array
     public static GameObject[] myObjects;

     //I used this to keep track of the number of objects I spawned in the scene.
     public static int numSpawned = 0;
 
     void Start()
     {
         //Important note: place your prefabs folder(or levels or whatever) 
             //in a folder called "Resources" like this "Assets/Resources/Prefabs"
         myObjects = Resources.LoadAll<GameObject>("Prefabs");
 
     }
 
 
 
     void SpawnRandomObject() 
     {    
         //spawns item in array position between 0 and 100
         int whichItem = Random.Range (0, 100);
 
     
         GameObject myObj = Instantiate (myObjects [whichItem]) as GameObject;
 
         numSpawned++;
 
         myObj.transform.position = transform.position;
     }
 
     void Update() 
     {
         if (numToSpawn > numSpawned) 
     {
             //where your instantiated object spawns from
             transform.position = new Vector3(0, 0, 0);

             SpawnRandomObject ();
         }
     }



How to populate a List with Prefabs from a folder:

     //define a list
     public static List <GameObject> myListObjects = new List<GameObject>();

     //I used this to keep track of the number of objects I spawned in the scene.
     public static int numSpawned = 0;
 
     void Start()
     {
         //Important note: place your prefabs folder(or levels or whatever) 
             //in a folder called "Resources" like this "Assets/Resources/Prefabs"
         Object[] subListObjects = Resources.LoadAll("Prefabs", typeof(GameObject));

             //This may be sloppy (I've only been programing for a short time) 
             //It works though :) 
             foreach (GameObject subListObject in subListObjects) 
     {    
         GameObject lo = (GameObject)subListObject;
         
         myListObjects.Add(lo);
     }
         startPosition = transform.position;
 
     }
 
 
 
     void SpawnRandomObject() 
     {    
         //spawns item in array position between 0 and 100
         int whichItem = Random.Range (0, 100);
     
         GameObject myObj = Instantiate (myObjects [whichItem]) as GameObject;
 
         numSpawned++;
 
         myObj.transform.position = transform.position;
     }
 
     void Update() 
     {
         if (numToSpawn > numSpawned) 
     {
             //where your instantiated object spawns from
             transform.position = new Vector3(0, 0, 0);

             SpawnRandomObject ();
         }
     }



Ok! Hopefully this makes this information easier to find - if you have suggestions to make this better let me know.


Also - if anyone can point me to a good explanation of using enums to sort that would be awesome.

Comment
Add comment · Show 5
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 Benproductions1 · May 23, 2014 at 12:21 PM 1
Share

Please post questions as questions and answers as answers. Don't post the solution to a problem in a question.

Also, all the information you have given is readily available through other sources, so I really don't see the point of more duplicate information... (Duplicate here)

avatar image Fritsl Benproductions1 · Jul 12, 2016 at 09:22 AM 1
Share

Please be nice to those who try and help. This page was extremely helpful to me.

avatar image glasstongue · May 25, 2014 at 05:06 AM 1
Share

I thought examples of dynamically adding objects to a list vs. array side by side might be helpful since there don't seem to be many examples of how to write it in context.

Sorry, I will not post in the future.

avatar image genocidalvirus · Oct 03, 2015 at 02:39 PM 0
Share

glasstongue this was very helpful. Thanks so much.

avatar image FullHeartGames · Apr 21, 2016 at 04:28 PM 1
Share

Glass Tongue thank you.

In case anyonewas wondeing I am in Unity 5.3 and the casting is a little different than other examples I have soon. At first I tried

WRONG

 $$anonymous$$yObjArr = Resources.LoadAll("path")as Gameobject[];

But Glass Tongue had it right with the cast

RIGHT

 $$anonymous$$yObjArr = Resources.LoadAll<Gameobject>("path");      

  



2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Journeythedev · Jul 21, 2017 at 07:13 PM

Extremely helpful! Thanks so much, I actually will refer people to this who have this question in class and on my team.

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
avatar image
0

Answer by LogicalCuber · Jun 07, 2020 at 06:24 AM

6 Years later ... and you make another guy happy hahhaha Many thanks!!!

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 TheOtherUserName · Jul 27, 2020 at 01:28 PM 0
Share

Yeah man same here helped a lot by making my little single slot inventory!

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

30 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 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

Fill an array with prefabs? 1 Answer

Accessing variable of instantiated prefab inside array in javascript 1 Answer

Clicker game instantiate prefab depending on gold per click 0 Answers

Do I necessarily need to instantiate a prefab before putting it in List to conserve his variable data? 0 Answers

Dictionary Keys to Stack Enemy Types 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