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
2
Question by FH_88 · Jan 11, 2015 at 04:08 AM · javascriptgameobjectrandomspawnamount

Spawn random amount of gameobjects

I have a script which spawns a gameobject every fourth second, but my problem is that it only spawns one object, I would like to spawn a random amount between two numbers for example 1 and 5. How would I do this?

My Script.

 var Mines : Transform;
 
 private var Timer : float;
 
 function Awake () {
     Timer = Time.time + 4;
 }
 
 function Update () {
     if (Timer < Time.time) {
         Instantiate(Mines, transform.position, transform.rotation);
         Timer = Time.time + 4;
         Debug.Log("Spawned Mine");
     }
 }
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
1

Answer by getyour411 · Jan 11, 2015 at 04:18 AM

Random.Range (1,6)

you probably want to do something with transform.pos or they'll all spawn at the same place

http://docs.unity3d.com/ScriptReference/Random.Range.html

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 FH_88 · Jan 11, 2015 at 09:51 PM 0
Share

Thank you for the great information, because right now the problem is that the amount of objects, spawn in the same place and I dont really get how I would fix this.

So far my script looks like this.

 var $$anonymous$$ines : Transform;
 
 private var Timer : float;
 
 function Awake () {
     Timer = Time.time + 4;
 }
 
 function Update () {
     
     var randNum = Random.Range(0, 10); // this will return a number between 0 and 9 
      var screenPosition : Vector3 = Camera.main.ScreenToWorldPoint(new Vector3(Random.Range(0,Screen.width), Random.Range(0,Screen.height), Camera.main.farClipPlane/2));
  
      if (Timer < Time.time) {
      
         for (var i = 0; i < randNum; i++){
           Instantiate($$anonymous$$ines, screenPosition, Quaternion.identity); 
 
         //Instantiate($$anonymous$$ines, transform.position, transform.rotation);
         Timer = Time.time + 4;
         Debug.Log("Spawned $$anonymous$$ines");
         
         }
     }
 }
avatar image
1

Answer by knuckles209cp · Jan 11, 2015 at 04:19 AM

 function Update() { 
 var randNum = Random.Range(0, 10); // this will return a number between 0 and 9 
 
 for (var i = 0; i < randNum; i++){
  Instantiate(Mines, transform.position, Quaternion.identity); 

//other stuff } }

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 FH_88 · Jan 11, 2015 at 09:52 PM 0
Share

Thank you very much for the great information.

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

26 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 avatar image avatar image avatar image avatar image

Related Questions

Spawn random amount apart of each other 1 Answer

Generate random number and set a GameObject active. 1 Answer

Spawn game object in random position on screen 1 Answer

Simple array an spawning question 6 Answers

When GameObject is spawned, rotate x 90. 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