Search Unity

[Solved] Pass static var to govern multiple spawned objects

Discussion in 'Scripting' started by ecnalyr, Jun 1, 2010.

  1. ecnalyr

    ecnalyr

    Joined:
    May 23, 2010
    Posts:
    38
    I have the following two scripts controlling the behavior of my bubble game objects. The intention is to have the bubbles appear randomly, then inflate (as if blown up) at random rates of speed to random sizes (some people blow bigger bubbles faster than others).:

    BlowBubble.js
    Code (csharp):
    1.  
    2. var startScale = Vector3.zero;
    3. var endScaleMax = Vector3(5, 5, 5);
    4. var endScaleMin = Vector3.one;
    5. var t = 0.0;
    6.  
    7.  
    8.    
    9. while (t<1.0) {//Bubbles are 'blown up' based on speed and size variables they receive from the BubbleSpawnerS.js
    10.    t += Time.deltaTime * BubbleSpawnerS.SPEED;
    11.    transform.localScale = Vector3.Lerp(startScale, BubbleSpawnerS.RANDOM1, t);
    12.    yield;
    13. }
    14.  
    BubbleSpawnerS.js
    Code (csharp):
    1.  
    2. static var TOTBUB = 0;
    3. var bubble : Transform;
    4. var MaxBubbles = 10;
    5. var MinTime = .1;
    6. var MaxTime = .8;
    7. var MinX = 0;
    8. var MaxX = 39;
    9. var MinY = 0;
    10. var MaxY = 14;
    11.  
    12. static var RANDOM1;
    13. static var SPEED = .1;
    14.  
    15. var minscale = 1;
    16. var maxscale = 5;
    17. var maxspeed = 1;
    18. var minspeed = .1;
    19.  
    20. var superz = Random.Range(minscale,maxscale);
    21. var randomgen = Vector3(superz,superz,superz);
    22. RANDOM1 = randomgen;
    23.  
    24. function Start ()
    25. {
    26.     while (true) {
    27.         if (TOTBUB < MaxBubbles) {//Script spawns bubbles at random positions at random points in time if we are not at maximum amount of bubbles
    28.             yield WaitForSeconds(Random.Range(MinTime, MaxTime));
    29.             var randomPos = Vector3 (Random.Range(MinX, MaxX), Random.Range(MinY, MaxY), 0);
    30.             Instantiate(bubble, randomPos, Quaternion.identity);
    31.             TOTBUB++;
    32.             //Script changes random static variables here that are read by BlowBubble.js which is attached to the bubble objects being spawned
    33.             //My intent is to have each newly spawned bubble object take this set of random variables and grow with them (like the bubbles are being blown up at different speeds to different sizes).
    34.             //My problem is that each bubble receives these updated variables each time a new bubble is spawned.
    35.             //So if a bubble is still growing due to a randomly slow growth time, then when a new bubble is spawned, both the new bubble and any other bubble that is still growing receives these variables.
    36.             //This results in my bubbles animating in a jittery fashion, each time a new bubble spawns all of the growing bubbles 'jump' to coincide with the new variables they are receiving
    37.             //This behavior stops on the bubble if it reaches it's maximum size before another spawn cycle
    38.             //Note that if I simply set the speed variable to a fixed fast speed that none of this becomes a problem, only my game isn't fun if just one bubble is spawning at a time.
    39.             var superz = Random.Range(minscale,maxscale);
    40.             var randomgen = Vector3(superz,superz,superz);
    41.             RANDOM1 = randomgen;
    42.             var speed = (Random.Range(minspeed, maxspeed));
    43.             SPEED=speed;
    44.         }
    45.         yield;
    46.     }
    47. }
    48.  
    49.  
    When the bubbles are spawned, they are passed static variables to control inflation speed and size.

    One bubble is spawned at a time, but additional bubbles are spawned as previous bubbles are still growing.

    If a bubble is still growing and another bubble is spawned, these static variables are read by the bubbles that are already growing and they 'jump' in size in a jittery fashion because of it. I want to eliminate this jittery jumping of size.

    How do I make the bubbles that are spawned keep their initial instructions to grow to a size dictated by the static variable at the time of them spawning?

    Would something involving private variables be plausible? I'm not too sure how they work, but couldn't I set a private var = static var as the static var comes into the script for the first time? Would this make just the one bubble keep it's original variables?
     
  2. barinelg

    barinelg

    Joined:
    Jun 1, 2010
    Posts:
    95
    Hello!

    I'm new to the forums, and to Unity itself, so I will do the best I can to answer this question.

    From the looks, your BlowBubble.js while loop is accessing the BubbleSpawnerS.js variables every clock cycle, while the BubbleSpawnerS is changing those same variables every clock cycle. My suggestion is that, before the BlowBubble while loop, to store those values in a variable (private or local to that function; both should work). This way, those values will never change. Or you can do it in Start().

    For example:
    Code (csharp):
    1.  
    2. private var speed;
    3. private var random1;
    4.  
    5. function Start() {
    6.     speed = BubbleSpawnS.SPEED;
    7.     random1 = BubbleSpawnerS.RANDOM1;
    8. }
    9.  
    10. //Function your while loop is in
    11. ...
    12. while (t<1.0) {
    13.    t += Time.deltaTime * speed;
    14.    transform.localScale = Vector3.Lerp(startScale, random1, t);
    15.    yield;
    16. }
    17.  
    18.  
    Or the following:

    Code (csharp):
    1.  
    2.  
    3. //Function your while loop is in
    4. ...
    5. var speed = BubbleSpawn.SPEED;
    6. var random1 = BubbleSpawnerS.RANDOM1;
    7.  
    8. while (t<1.0) {
    9.    t += Time.deltaTime * speed;
    10.    transform.localScale = Vector3.Lerp(startScale, random1, t);
    11.    yield;
    12. }
    13.  
    I'm a little rusty on my Javascipting, but I think that's the gist of it.

    Hope this helps!
     
  3. ecnalyr

    ecnalyr

    Joined:
    May 23, 2010
    Posts:
    38
    Fixed it, I will post my results in case anyone in the future could find it usefull:

    The answer was to use the static variables to declare private variables on a per-bubble basis.

    BlowBubbles.js
    Code (csharp):
    1.  
    2. var startScale = Vector3.zero;
    3. var endScaleMax = Vector3(5, 5, 5);
    4. var endScaleMin = Vector3.one;
    5. var t = 0.0;
    6. //private variables needed here to record the static variables at the moment the bubble object was spawned.
    7. private var recentspeed = .1;
    8. private var recentrandom1;
    9.    recentspeed=BubbleSpawnerS.SPEED;
    10.    recentrandom1=BubbleSpawnerS.RANDOM1;
    11.  
    12.    
    13. while (t<1.0) {//Bubbles are 'blown up' based on speed and size variables they receive from the BubbleSpawnerS.js
    14.    t += Time.deltaTime * recentspeed;
    15.    transform.localScale = Vector3.Lerp(startScale, recentrandom1, t);
    16.    yield;
    17. }
    18.  
    BubbleSpawnerS.js
    Code (csharp):
    1.  
    2. static var TOTBUB = 0;
    3. var bubble : Transform;
    4. var MaxBubbles = 10;
    5. var MinTime = .1;
    6. var MaxTime = .8;
    7. var MinX = 0;
    8. var MaxX = 39;
    9. var MinY = 0;
    10. var MaxY = 14;
    11.  
    12. static var RANDOM1;
    13. static var SPEED = .01;
    14.  
    15. var minscale = 1;
    16. var maxscale = 5;
    17. var maxspeed = 1;
    18. var minspeed = .1;
    19.  
    20. var superz = Random.Range(minscale,maxscale);
    21. var randomgen = Vector3(superz,superz,superz);
    22. RANDOM1 = randomgen;
    23.  
    24. function Start ()
    25. {
    26.     while (true) {
    27.         if (TOTBUB < MaxBubbles) {//Script spawns bubbles at random positions at random points in time if we are not at maximum amount of bubbles
    28.             yield WaitForSeconds(Random.Range(MinTime, MaxTime));
    29.             var randomPos = Vector3 (Random.Range(MinX, MaxX), Random.Range(MinY, MaxY), 0);
    30.             Instantiate(bubble, randomPos, Quaternion.identity);
    31.             TOTBUB++;
    32.             //Script changes random static variables here that are read by BlowBubble.js which is attached to the bubble objects being spawned
    33.             //My intent is to have each newly spawned bubble object take this set of random variables and grow with them (like the bubbles are being blown up at different speeds to different sizes).
    34.             //My problem is that each bubble receives these updated variables each time a new bubble is spawned.
    35.             //So if a bubble is still growing due to a randomly slow growth time, then when a new bubble is spawned, both the new bubble and any other bubble that is still growing receives these variables.
    36.             //This results in my bubbles animating in a jittery fashion, each time a new bubble spawns all of the growing bubbles 'jump' to coincide with the new variables they are receiving
    37.             //This behavior stops on the bubble if it reaches it's maximum size before another spawn cycle
    38.             //Note that if I simply set the speed variable to a fixed fast speed that none of this becomes a problem, only my game isn't fun if just one bubble is spawning at a time.
    39.             var superz = Random.Range(minscale,maxscale);
    40.             var randomgen = Vector3(superz,superz,superz);
    41.             RANDOM1 = randomgen;
    42.             var speed = (Random.Range(minspeed, maxspeed));
    43.             SPEED=speed;
    44.         }
    45.         yield;
    46.     }
    47. }
    48.  
    49.