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
1
Question by Ant0n1 · Dec 09, 2013 at 11:32 AM · objectrandomspawnlocation

Spawn Object On Empty Object Location Different Each Time Game Starts

Hello everyone, I'm working on a first person horror game and i found a script some days ago which was just what i was looking for but for some reason i can't find it again. Can you please help me ? Let's say i have 3 empty game objects on my scene, choose a game object from the script (source) and then let the script chose one of the 3 empty game objects randomly to spawn the source object but i want that to change every time i start the game.

     var prefab : GameObject;
     var spawn1 : GameObject;
     var spawn2 : GameObject;
     var spawn3 : GameObject;
     public GameObject[spawn1, spawn2, spawn3] spawnPoints;
     public int randomNum;
      
     function Start ()
      
     randomNum = Random.Range(0,3); // generates random number between 0 and 3.
     {
     //SpawnerID = Random.Range(1, 3);
      
     if(spawn1){
      
     transform.position = spawnPoints[randomNum].transform.position;
      
     }
      
     if(spawn2){
      
     transform.position = spawnPoints[randomNum].transform.position;
      
     }
      
     if(spawn3){
      
     transform.position = spawnPoints[randomNum].transform.position;
      
     }
     }
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

2 Replies

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

Answer by hirenkacha · Dec 09, 2013 at 11:38 AM

Assign that 3 empty GameObjects to the spawnPoints array in the inspector.

 public GameObject[] spawnPoints;
 public int randomNum; 
 
 void Start()
 {
    randomNum = Random.Range(0,3); // generates random number between 0 and 3. 
    transform.position = spawnPoints[randomNum].transform.position;
 }

For JS you can use

 public var spawnPoints : GameObject[] ;
 public var randomNum : int; 
 
 function Start()
 {
    randomNum = Random.Range(0,3); // generates random number between 0 and 3. 
    transform.position = spawnPoints[randomNum].transform.position;
 }
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 Ant0n1 · Dec 09, 2013 at 11:54 AM

I get a lot of errors, here is the script now

 var prefab : GameObject;
 var spawn1 : GameObject;
 var spawn2 : GameObject;
 var spawn3 : GameObject;
 public GameObject[spawn1, spawn2, spawn3] spawnPoints;
 public int randomNum;
 
  function Start () 
      
      randomNum = Random.Range(0,3); // generates random number between 0 and 3.
      {
     //SpawnerID = Random.Range(1, 3);
      
     if(spawn1){
      
     transform.position = spawnPoints[randomNum].transform.position;
      
     }
      
     if(spawn2){
      
     transform.position = spawnPoints[randomNum].transform.position;
      
     }
      
     if(spawn3){
      
     transform.position = spawnPoints[randomNum].transform.position;
      
     }
     }
     
Comment
Add comment · Show 7 · 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 hirenkacha · Dec 09, 2013 at 11:58 AM 0
Share

Always add aditional information in question, not as an answer.

avatar image Ant0n1 · Dec 09, 2013 at 12:01 PM 0
Share

Sorry, your right i will fix it.

avatar image hirenkacha · Dec 09, 2013 at 12:01 PM 0
Share

Dont mix javascript and c# together. Either use js or c#. I have given you the script in cs. Just try it...

avatar image Ant0n1 · Dec 09, 2013 at 12:03 PM 0
Share

Well, i have never used C, can you tell me the script or at least the java version.

avatar image hirenkacha · Dec 09, 2013 at 12:15 PM 0
Share

I have already given answer to this question in both js and c#. Let me know if any problem.

Show more comments

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

17 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

Related Questions

Randomly Initiate Cubes - Prevent Same Position 0 Answers

Spawning different random objects at the same position? 2 Answers

Spawning Objects at location 3 Answers

Spawn Objects at Different Locations with Tags? 2 Answers

How can i delete an item from an array after being used? (So, it won't be repeated) 2 Answers


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