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 Hexer · Apr 30, 2014 at 04:34 PM · javascriptinstantiatespawn

Assign the Instantiated Object to an Empty Object.

With this Script I made,I can Instantiate the Objects. The only problem is that it haven't been given a proper spawn location yet. I got some Empty gameobjects in the scene (the spawn location). My plan was when the Random.range(0,4)chose a number between 0 and 4 It would instantiate the object.This all works fine but as I mentioned I can't give it a proper spawn location :)

don't look at the if(GameObject.Find("Player").GetComponent(Rigidbody2D).isKinematic == false){ This was nessecary so that it wouldn't instantiate right from the start

 //Several SpawnPoints
 var Spawn1 : GameObject;
 var Spawn2 : GameObject;
 var Spawn3 : GameObject;
 var Spawn4 : GameObject;
 var Spawn5 : GameObject;
 //The Obstacle
 var Obstacles : GameObject;

 //Repeats the function every second
 InvokeRepeating("Start2", 0, 1.0);
 
 function Start2()            
 {                 
 if(GameObject.Find("Player").GetComponent(Rigidbody2D).isKinematic == false){
 
  var Spawn = Random.Range(0,4);
  
  
   if(Spawn == 0){
   Debug.Log("spawn0");
  Instantiate(Obstacles);
  }
   if(Spawn == 1){ 
   Debug.Log("spawn1");
    Instantiate(Obstacles);
  }
   if(Spawn == 2){ 
   Debug.Log("spawn2");
     Instantiate(Obstacles);
  }
   if(Spawn == 3){ 
   Debug.Log("spawn3");
   Instantiate(Obstacles);
  }
   if(Spawn == 4){ 
   Debug.Log("spawn4"); 
   Instantiate(Obstacles);
  }
  }
  }

Thanks for reading, I hope you can help me out.

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
1
Best Answer

Answer by iamvishnusankar · Apr 30, 2014 at 04:52 PM

Declare the spawn points as array of objects and try rewriting your script as follows. This is C# version, Only some minor declaration tweaks would be required to convert it to JavaScript.

 //Array of objects holds the spawn point positions
     public GameObject[] spawnPoints;
 
     public void Start2 () 
     {
     
         if(GameObject.Find("Player").GetComponent(Rigidbody2D).isKinematic == false)
         {
             //Call the function to instantiate at points
             Spawn(Random.Range(0,spawnPoints.Length));
         
         }
     }
     
 
     void Spawn (int i)
     {
         Transform currPos = spawnPoints[i].transform;
         Instantiate (obstacles,currPos.position,Quaternion.identity);
     }
Comment
Add comment · Show 3 · 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 Hexer · May 05, 2014 at 12:52 PM 0
Share

Yeah it worked thx, It was a little bit confusing because I didnt understand why you made

 Transform currPos = spawnPoints[i].transform

when I also just could do

 Instantiate(Obstacles,Spawn1.transform.position,Quaternion.indentity);
avatar image iamvishnusankar · May 05, 2014 at 02:55 PM 0
Share
 Transform currPos = spawnPoints[i].transform

It deter$$anonymous$$es on which position the "obstacle" get spawned. Since, we have an array of SpawnPoints we need to deter$$anonymous$$e on which position the object should get Spawned. The "i" value selects a random SpawnPoint from array of gameobjects and the value is being assigned to a temp Transform(currPos). And this temp value is used to deter$$anonymous$$e on which position the Obstacles are instantiated.

So no need of writing like , Spawn1.transorm.position, Spaw2.transform.position etc . . as the above script will deter$$anonymous$$e the points by itself.

avatar image Hexer · May 05, 2014 at 03:22 PM 0
Share

Well it works fine for me now with

 Instantiate(Obstacles,Spawn1.transform.position,Quaternion.indentity);

So I wont change something that isnt broke,but thanks for the comment. It helped me understand more of the script. ;)

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

22 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

Related Questions

Enemy spawn script not working properly ! 1 Answer

instantiate a set amount 3 Answers

Issue With Spawning Enemies (javascript) 2 Answers

Machine gun script shoots all bullets at once 2 Answers

Script needs fixing ?! 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