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 · Sep 29, 2011 at 06:04 PM · arrayrandomspawn

random spawn locations....

What can I change or add to my script to have prefab objects randomly spawn from three locations. I don't need to put forward velocity to the objects spawned because they have AI script on the objects already. I just need them to be dropped from a point in space.

The Code:

 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 = Mathf.Abs(Random.Range(0,3));
 
     instance = Instantiate(game_cube, spawn[randomPick].position, spawn[randomPick].rotation);
     instance.velocity = transform.TransformDirection( Vector3( 0, 0, 0 ) );
     cube_count = cube_count +1;
 
 }
Comment
Add comment · Show 2
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 aldonaletto · Sep 29, 2011 at 06:10 PM 0
Share

What's wrong? This code should be doing what you expect. The only weird thing is the $$anonymous$$athf.Abs function: you don't need it, since Random.Range(0, 3) returns a random number between 0 and 2 (the max limit never is reached in the int version of Random.Range).
Another thing: you can just do instance.velocity = Vector3.zero;, since the zero vector has no direction to transform.

avatar image Beta_Artist · Sep 29, 2011 at 07:01 PM 0
Share

I'm getting an error saying: IndexOutOfRangeException: Array index is out of range. SpawnController.LaunchProjectile () (at Assets/Standard Assets/SpawnController.js:19)

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Martijn Hendriks · Oct 01, 2011 at 08:50 AM

I'm not good with javascript, but I think it has something to do with the initialisation of our spawn array. Are you sure that this array is created and filled correctly, since your declaration doesn't specify the size of the array an what is inside its entries? Have you set your spwan locations in the inspector?

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 SHG · May 17, 2015 at 06:36 PM

I wrote a random spawner script a while back, so here ya go!

 var timer : float = 0;
 var timeofwait : float = 0.25;
 var spawning : boolean = false;
 var prefab : GameObject;
 var spawn1 : Transform;
 var spawn2 : Transform;
 var spawn3 : Transform;
 var spawn4 : Transform;
 var prefab1 : GameObject;
 var prefab2 : GameObject;
 var prefab3 : GameObject;
 var prefab4 : GameObject;
 var soundeffect: AudioClip;//spawn
 
 function Update () {
  //check if spawning at the moment, if not add to timer
  if(!spawning){
   timer += Time.deltaTime;
  }
  //when timer reaches 2 seconds, call Spawn function
  if(timer >= 1){
   Spawn();
   GetComponent.<AudioSource>().clip = soundeffect;
   GetComponent.<AudioSource>().volume = 1;
   GetComponent.<AudioSource>().Play();
  }
 }
  
 function Spawn(){
  var randomvolume : float = Mathf.Abs(Random.Range(0.35,1));
  //set spawning to true, to stop timer counting in the Update function
  spawning = true;
  //reset the timer to 0 so process can start over
  timer = 0;
  
  //select a random number, inside a maths function absolute command to ensure it is a whole number
  var randomPick : int = Mathf.Abs(Random.Range(1,5));
  var randomPrefab : int = Mathf.Abs(Random.Range(1,5));
  //create a location 'Transform' type variable to store one of 3 possible locations declared at top of script
  var location : Transform;
  
  //check what randomPick is, and select one of the 3 locations, based on that number
  if(randomPick == 1){
   location = spawn1;
   prefab = prefab1;
  }
  else if(randomPick == 2){
   location = spawn2;
   prefab = prefab2;
  }
  else if(randomPick == 3){
   location = spawn3;
   prefab = prefab3;
  }
   else if(randomPick == 4){
   location = spawn4;
   prefab = prefab4;
  }
  //create the object at point of the location variable
  var thingToMake = Instantiate(prefab, location.position, location.rotation);
  
  //halt script for 1 second before returning to the start of the process
  yield WaitForSeconds(timeofwait);
  //set spawning back to false so timer may start again
  spawning = false;
 }

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Random textures... 1 Answer

Random select from array and spawn 1 Answer

How to fix some of location have more than 1 object? 2 Answers

Spawning targets on 2 of 4 pre-defined locations using an array and empty game objects? 1 Answer

Spawn object from Random Vector3 in array 0 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