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 taiomi · Mar 25, 2014 at 09:31 PM · function updatevar type

How to stop spawn after a limit (int)

Help on this question would be greatly appreciated - I am wondering if it possible to limit the amount of balls that are spawned?

The code is below. Thanks in advance

 var ball : GameObject;
 var spawn_position;
 var timer = 0.0;
 
 function spawn_ball ()
 
 {
 
 spawn_position = Vector3 (-2.77,3.3,0);
 
 var temp_spawn_ball = Instantiate (ball, spawn_position, Quaternion.identity);
 
 }
 function Start () {
 
 }
 
 function Update () {
 
 timer += Time.deltaTime;
 
 if(timer > 8)
 
 {
 
 spawn_ball();
 timer = 0.0;
 
 }
 
 }
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
0
Best Answer

Answer by ransomink · Mar 26, 2014 at 02:41 AM

You need to create a variable to hold the total amount of spawned balls and a variable for the current amount of spawned balls. Also, use a variable in place of '8' when checking the timer because you might change this down the line, and it is easier to do so at the top, in one spot than multiple areas of your script.

I wrapped everything inside a boolean check because when the max amount of GameObjects are met the timer will keep going and this probably eats up resources. So, by turning the boolean off when the spawned limit is met, the timer will cease.

 public var ball : GameObject;// the GameObject representing 'ball' (to be spawned)
 public var spawnPosition : Vector3;// the position of the spawned GameObject
 public var spawnLimit : int;// the max amount of GameObjects that can be spawned
 public var spawnCount : int;// the current amount of spawned GameObjects
 public var spawn : boolean;// if the GameObjects are allowed to spawn
 public var waitTime : float;// the amount of time (delay) to wait before re-spawning
 public var timer : float = 0.0;// the current time in seconds
 
 function Start ()
 {
     
 }
 
 function Update ()
 {
     // check if GameObjects (the ball) can be spawned
     if ( spawn )
     {
         timer += Time.deltaTime;
         
         // check if the timer has reached the wait time
         if ( timer > waitTime )
         {
             // check if the current spawn amount is below the limit
             if ( spawnCount < spawnLimit )
             {
                 Spawn ();// spawn the GameObject
                 timer = 0.0;// reset the timer
             }
             else
             {
                 spawn = false;
                 Debug.Log ( "Spawn Limit Has Been Reached!" );
             }
         }
     }
 }
 
 // spawn the GameObject
 function Spawn ()
 {
     spawnPosition = Vector3 ( -2.77, 3.3, 0 );// set the position for the spawned GameObject
     Instantiate ( ball, spawnPosition, Quaternion.identity );// clone/duplicate the given object
     spawnCount++;// add to the current amount of spawned GameObjects (in your case, the number of balls)
     Debug.Log ( "Spawned GameObject #" + spawnCount );
 }

You will run into a problem with your script though. Whenever a GameObject (ball) is spawned, it will appear in the same spot because you're explicitly setting its position. Unless the GameObject (ball) is moving before the next spawn, you should use a random position.

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 taiomi · Mar 26, 2014 at 11:29 PM 0
Share

Thank you so much the help this is perfect! The detailed idiot proof comments in the code are exactly the way I would like an answer to be :) I love you!

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

21 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

Related Questions

Why does this for loop crash my unity? 1 Answer

Cooldown button system 1 Answer

How to remove decimals from Vector3.Distance 3 Answers

How to make spawn parameters 0 Answers

Spawning in vicinity? 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