Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Beta_Artist · Oct 03, 2011 at 07:37 PM · gameobjectarrayspawn

Multiple spawn points using Array...

I'm trying to make a spawn script that would randomly choose one of three spawn points which are located at three game objects. This is the first set of code I tried.

 var spawn : Transform[]; 
 var game_cube : Rigidbody;
 var cube_count = 0;
 
 InvokeRepeating("LaunchProjectile", 2, 3);
 function Update() {
     if (cube_count>= 10) {
        CancelInvoke();
     }
 
 }
 
 function LaunchProjectile () {
     var randomPick : int = Random.Range(0,2);
 
     instance = Instantiate(game_cube, spawn[randomPick].position, transform.rotation);
     instance.velocity = Vector3.zero;
     cube_count = cube_count +1;
 
 }

It has no errors but the prefab objects I made are not spawning at all. So I tried making a basic script that could make it at least functional.

 var game_cube : Rigidbody;
 var spawn : Transform[];
 var cube_count = 0;
 var respawn1 : GameObject;
 var respawn2 : GameObject;
 var respawn3 : GameObject;
 
 InvokeRepeating("LaunchProjectile", 2, 3);
 function Update() {
     if (cube_count>= 10) {
         CancelInvoke();
     }
 
 }
 
 function LaunchProjectile () {
     var randomPick : int = Random.Range(0,2);
         if(randomPick : 0){
             spawn : respawn1;
         }
         if(randomPick : 1){
             spawn : respawn2;
         }
         if(randomPick : 2){
             spawn : respawn3;
         }
         
     instance = Instantiate(game_cube, spawn.position, spawn.rotation);
     instance.velocity = Vector3.zero;
     cube_count = cube_count +1;
 
 }
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 DaveA · Oct 03, 2011 at 10:27 PM 0
Share

I assume you assigned 3 transforms to 'spawn' with the Inspector, as well as game_cube?

1 Reply

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

Answer by vxssmatty · Oct 03, 2011 at 10:31 PM

You were on the right track, but you havnt started the invoke command at all, it has to be in start or an awake function;

And then you would notice that your instance was not a variable, you have to assign it like you did with the random value variable.

And you can just do cube_count++; to increment the variable by 1 :)

 var spawn : Transform[]; //array of spawn points
 var game_cube : Rigidbody; //the game cube
 var cube_count = 0; //cube counter
 
 function Start () { //runs once at instantiation
 InvokeRepeating("LaunchProjectile", 2, 3);
 }
 
 function Update() {
     if (cube_count>= 10) {
        CancelInvoke();
     }
 
 }
 
 function LaunchProjectile () {
     var randomPick : int = Random.Range(0,2);
 
     var instance = Instantiate(game_cube, spawn[randomPick].position, transform.rotation);
     instance.velocity = Vector3.zero;
     cube_count++;
 
 }
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 BardenMakesAGame · Mar 12, 2013 at 10:24 PM 0
Share

How would you go about changing this code to make it so that a given spawn point can only have one Game Object spawn in it?

EDIT: Also, spawn[randomPick] throws up the error stating that 'spawn' is undefined.

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Spawning GameObjects with help of classes --- Attaching classes to GameObjects 1 Answer

Random textures... 1 Answer

Making an Array of Spawnpoints... 1 Answer

Array problem? help please! 1 Answer

Unity Engine issue... 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