Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 TimBorquez · Mar 27, 2013 at 02:07 AM · instantiaterandomarraysresource

can you instantiate random prefabs from the resource folder?

im going to be spawning a lot of different prefabs so I want to be able to just drop them in the folder

i just learned how to instantiate from the resource folder

 var instance : GameObject = Instantiate(Resources.Load("prefab name"));

now I'm just trying to figure out how I can instantiate random objects from the folder. Is it even possible to instantiate without the name of the object? thanks

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 TimBorquez · Mar 27, 2013 at 02:06 AM 0
Share

maybe i could somehow make an array of the names and then instantiate a random array number? i don't know what i'm doing...

2 Replies

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

Answer by whydoidoit · Mar 27, 2013 at 02:17 AM

Put the prefabs in a sub folder of resources called "Prefabs" then

  var prefabs : Object[] = Resources.LoadAll("Prefabs");

  function SpawnRandom() : GameObject
  {
      var toSpawn : GameObject = prefabs[Random.Range(0, prefabs.Length)];
      var spawned = Instantiate(toSpawn);
      return spawned;
  }
Comment
Add comment · Show 5 · 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 whydoidoit · Mar 27, 2013 at 02:23 AM 0
Share

Actually I'd advise that it's better practice to just have a:

  var prefabs : GameObject[];

And just assign them in the inspector. Then use that SpawnRandom function - this way you still have a single place to drop them - but go ahead and use Resources if it's a better work flow for you.

avatar image TimBorquez · Mar 27, 2013 at 02:50 AM 0
Share

thank you, this helps a lot

avatar image whydoidoit · Mar 27, 2013 at 09:02 AM 0
Share

Him Tim, can you click the tick next to either Robert's or my answer, depending on which one you choose? It gets us the karma and keeps the board clean.

avatar image TimBorquez · Mar 28, 2013 at 12:53 AM 0
Share

oh i didnt know how to do that, ok done went back and fixed all my questions, didnt know that was the thing to do

avatar image blackbrosg · Sep 21, 2021 at 05:42 AM 0
Share

i need load this maps buttons randomly, can u help me?

 void LoadMaps ()
     {
         // Get array of all maps.
         TextAsset[] mapNames = Resources.LoadAll<TextAsset>("Maps");
 
         // Create button and set text and onClick event.
         foreach(TextAsset map in mapNames)
         {
             GameObject mapObj = Instantiate(mapButtonPrefab, mapContainer.transform);
             mapObj.GetComponentInChildren<Text>().text = map.name;
 
             mapObj.GetComponent<Button>().onClick.AddListener(() => { OnSelectMap(map.name); });
         }
     }
avatar image
0

Answer by robertbu · Mar 27, 2013 at 02:17 AM

If you named them with a number like, "prefab0", "prefab1",..."prefab24", you could do something like:

 var name = "prefab"+Random.Range(0, 25);

Then you can instantiate using the generated name. Note Random.Range() of integers is exclusive (does not include) the last value.

If don't want to name them with an order, you can build an array and assign the strings in the inspector.

Note this method will result in duplicates. That is each time you instantiate you will have an equal chance of selecting any string including any that have already been selected.

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 TimBorquez · Mar 27, 2013 at 02:50 AM 0
Share

thank you too

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

12 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

Related Questions

How do you spawn a random object from an array. 2 Answers

Three Spots For Three Random Objects 1 Answer

Instantiate random object from Array 2 Answers

Three Random Objects From Array Without Repeat 1 Answer

Instantiate a random item at a random position 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