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 FatalNightmare · Mar 15, 2015 at 01:43 AM · prefablistrandomspawn

Spawn random prefabs at multiple locations

Hello, I am trying to randomly generate a city. I have 20 points in the world and with one script I'd like to randomly generate a building from a list of 10 at each one of those points. I am stuck and don't know where to go with the code. I have a code but all it is able to do is randomly spawn a building at one location (the location which has this code) I need it to be able to randomly spawn a building (different buildings) at 20 different locations. Any assistance helps! Thank you! Code:

 //The list of the 20 spawn points
 public var Block : Transform [];
 
 public var ObjectSpawned : GameObject;
 
 //The list of the 10 buildings to randomly spawn at each spawn point
 public var ObjecttoSpawn1 : GameObject;
 public var ObjecttoSpawn2 : GameObject;
 public var ObjecttoSpawn3 : GameObject;
 public var ObjecttoSpawn4 : GameObject;
 public var ObjecttoSpawn5 : GameObject;
 public var ObjecttoSpawn6 : GameObject;
 public var ObjecttoSpawn7 : GameObject;
 public var ObjecttoSpawn8 : GameObject;
 public var ObjecttoSpawn9 : GameObject;
 public var ObjecttoSpawn10 : GameObject;
 
 var TimeToSpawn : int;
 var RandomNumber : int;
 RandomNumber = Random.Range(1.0, 5.0);
 TimeToSpawn = Random.Range(1.0,10.0);
 
 function Update () 
 {
 if(Input.GetKeyDown(KeyCode.Space)) 
 {
 Invoke ("spawnObject", TimeToSpawn); //Spawn an object after time and once space is pressed
 }
 }
 
 
 
 function Start () 
 {
 //These designate an object to a number when the random number is picked
 //I would like the random number to reshuffle so that there is a random chance for each building to be spawned at each spawn point
 if (RandomNumber == 1)
 {
     ObjectSpawned = ObjecttoSpawn1;
 }
 if (RandomNumber == 2)
 {
     ObjectSpawned = ObjecttoSpawn2;
 }
 if (RandomNumber == 3)
 {
     ObjectSpawned = ObjecttoSpawn3;
 }
 if (RandomNumber == 4)
 {
     ObjectSpawned = ObjecttoSpawn4;
 }
 if (RandomNumber == 5)
 {
     ObjectSpawned = ObjecttoSpawn5;
 }
 if (RandomNumber == 6)
 {
     ObjectSpawned = ObjecttoSpawn6;
 }
 if (RandomNumber == 7)
 {
     ObjectSpawned = ObjecttoSpawn7;
 }
 if (RandomNumber == 8)
 {
     ObjectSpawned = ObjecttoSpawn8;
 }
 if (RandomNumber == 9)
 {
     ObjectSpawned = ObjecttoSpawn9;
 }
 if (RandomNumber == 10)
 {
     ObjectSpawned = ObjecttoSpawn10;
 }
 }//Start Function end bracket
 
 function spawnObject()
 {
 //Spawn Object at curret location
 //I do not know how to code it so that it spawns 20 different times
 Instantiate (ObjectSpawned, transform.position, transform.rotation); 
 }
 
Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Fanttum · Mar 15, 2015 at 03:03 AM

Sounds like you need a For Loop somewhere in there.

Not quite the same thing, but this tutorial might also be helpful.

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

20 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

Related Questions

i need Spawn Object loop if find object tag ("Clone") in list ,when i delete find object tag ,cript is good but no find object, please help me 1 Answer

spawning random objects at 2 defined points 1 Answer

Random element from the list 1 Answer

Problem with random spawning and coroutine 1 Answer

Random select from array and spawn 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