Search Unity

Problems with WaitForSeconds

Discussion in 'Scripting' started by Digital-Phantom, Dec 21, 2014.

  1. Digital-Phantom

    Digital-Phantom

    Joined:
    May 31, 2013
    Posts:
    42
    Im trying to make it so only one gameObject (in this case a boulder) is present in game at any given time.

    My player enters a trigger which instantiates the BoulderSpawn object, this in turn creates a boulder. However Im getting a constant stream of boulders, not just a single one.
    I know this is due to functionUpdate I just can't quite figure how limit the number of created objects.
    I have this script attached to the BoulderSpawn object

    Code (JavaScript):
    1. #pragma strict
    2.  
    3. var Boulder : GameObject;
    4. var BoulderLimit = 0;
    5.  
    6. function Update()
    7.  
    8. {
    9.     if(BoulderLimit <= 0);
    10.     {
    11.         Instantiate(Boulder, transform.position, transform.rotation);
    12.     }
    13. }
    (I'm fairly sure its something wrong with my coding)

    Any ideas guys?
     
  2. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    2,824
    Not the best solution, but in order to stop instantiating based on your code you'd need to increase the count in the if-statement so that the condition will be false and decrease it again when you want to spawn the next one.

    Should the Spawner keep spawning boulders later on although you're not entering triggers anymore? If not, i'd simply go without the spawner and spawn the boulder as soon as you enter the trigger.
    If not i'd rather wrap that up in methods, so whenever you destroy a Boulder it would decrease the count so that a new one can be spawned, and whenever a new will be created it will increase the count.
     
    Digital-Phantom likes this.
  3. Digital-Phantom

    Digital-Phantom

    Joined:
    May 31, 2013
    Posts:
    42
    OK I've kinda cheated and used a script from elseware -

    Code (JavaScript):
    1. #pragma strict
    2.  
    3. public var Boulder : GameObject;
    4. public var BoulderLimit : int; // the max amount of Boulders that can be spawned
    5. public var BoulderCount : int; // the current amount of spawned Boulders
    6. public var spawnBoulder : boolean; // if the Boulders are allowed to spawn
    7.  
    8. function Update ()
    9. {
    10.                
    11.     if ( BoulderCount < BoulderLimit ) // check if the current spawn amount is below the limit
    12.     {
    13.         SpawnBoulder (); // spawn the Boulder
    14.     }
    15.          
    16.     else
    17.          
    18.     {
    19.         spawnBoulder = false;
    20.         Debug.Log ( "Spawn Limit Has Been Reached!" );
    21.     }
    22. }
    23. function SpawnBoulder () // spawn the GameObject
    24. {
    25.      Instantiate(Boulder, transform.position, transform.rotation);
    26.      BoulderCount++; // add to the current amount of spawned Boulders
    27.     Debug.Log ( "Spawned GameObject #" + BoulderCount );
    28.  
    29. }
    However now I'm only getting one boulder no matter how many times I enter the trigger.
    I think this is due to what you said about decreasing the count as I have nothing (code wise) to tell the count to decrease.
    My boulders have a destroy script on them and self destruct after 7 seconds

    Am I right in thinking I'd have to change the Boulder destroy script so as it also decreases the count?
    That means having to call a variable from another script right? (no clue as to how to do this btw)

    OR

    Could I just add a WaitForSeconds (say 8 secs) and then reset the count to zero?
     
    Last edited: Dec 21, 2014
  4. Digital-Phantom

    Digital-Phantom

    Joined:
    May 31, 2013
    Posts:
    42
    I was hoping this would work but it just doesn't seem to run the newFunction part of the script
    Code (JavaScript):
    1. #pragma strict
    2.  
    3. public var Boulder : GameObject;
    4. public var BoulderLimit : int; // the max amount of Boulders that can be spawned
    5. public var BoulderCount : int; // the current amount of spawned Boulders
    6. public var spawnBoulder : boolean; // if the Boulders are allowed to spawn
    7.  
    8. function Update ()
    9. {
    10.                
    11.     if ( BoulderCount < BoulderLimit ) // check if the current spawn amount is below the limit  
    12.     {
    13.         SpawnBoulder (); // spawn the Boulder
    14.     }
    15.            
    16.     else
    17.            
    18.     {
    19.         spawnBoulder = false;
    20.         Debug.Log ( "Spawn Limit Has Been Reached!" );
    21.     }
    22.  
    23. }
    24. function SpawnBoulder () // spawn the GameObject
    25. {
    26.     Instantiate(Boulder, transform.position, transform.rotation);
    27.     BoulderCount++; // add to the current amount of spawned Boulders
    28.     Debug.Log ( "Spawned GameObject #" + BoulderCount );
    29.     newFunction();  
    30. }
    31.  
    32. function newFunction()
    33.  
    34. {
    35.     yield WaitForSeconds(8);
    36.     BoulderCount--;
    37.     Debug.Log ( "Boulder Count Reset!" );
    38. }
    39.  
    Any suggestions?
     
  5. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    2,824
    I
    Not sure about JavaScript/UnityScript but you may want to run that as a Coroutine.

    Anyway, how many spawner do you need in your game, more than one at the same time? You could think off a design to allow your boulders to adjust the counter. This could be more flexible when you want to increase the total amount for some reason later on.

    E.g. if you only have one spawner at any given time, you could work with statics. If that's not what you prefer, you could give the boulder script a variable of the spawner script and assign it in the start method. You can then increase the current amount via that reference and when you despawn them. you can decrease it. Just as mentioned above. You've almost got total freedom for your desired design at this point. There are several ways to achieve that and it always depends on your actual need and the rest of your game.