Search Unity

bool lean asign problem

Discussion in 'Scripting' started by love_candy, Aug 28, 2014.

?

is the code good?

  1. no

    3 vote(s)
    100.0%
  2. yes

    0 vote(s)
    0.0%
  1. love_candy

    love_candy

    Joined:
    Aug 19, 2014
    Posts:
    17
    hello i want to create 1 object then wait 2s and create object again in a loop. but with my code i get thousends of obects in instant . whats going on. i know its easy but still I have hard time with this. can sombody point me in right direction?
    Code (JavaScript):
    1. #pragma strict
    2. var  kabum:boolean=false;
    3. var wybuch :GameObject;
    4.  
    5. function Start () {
    6.  
    7. }
    8.  
    9. function Update () {
    10. if(kabum==false){
    11. bum();
    12. kabum=true;
    13. }
    14.  
    15. if(kabum==true){
    16. wait();
    17. }
    18.  
    19. }
    20.  
    21.      
    22.        function bum()
    23. {
    24. var instanceBullet = Instantiate(wybuch,transform.position,transform.rotation);
    25. }
    26.  
    27.  
    28.     function wait(){
    29.      yield WaitForSeconds(5);
    30. kabum=false;}
    31.  
    32.  
    33.  
    34.    
     
  2. Polymorphik

    Polymorphik

    Joined:
    Jul 25, 2014
    Posts:
    599
    Don't have it in an update function. I am not familiar with UnityScript but, if you can access a method externally, I would have a method that starts a coroutine that spawns your objects.
     
  3. Polymorphik

    Polymorphik

    Joined:
    Jul 25, 2014
    Posts:
    599
    If not, try this.

    var delay : float = 5.0f;
    var timeElapsed: float = 0.0f;

    function Update() {
    if(timeElapsed >= delay) {
    bum();
    timeElapsed = 0.0f;
    }

    timeElapsed += Time.deltaTime;
    }

    That should work, I script in C# not UnityScript but the logic should still be the same.
     
  4. love_candy

    love_candy

    Joined:
    Aug 19, 2014
    Posts:
    17
    the real thing is why function wait doest change kabum to false when is called ;/
    1. function wait(){
    2. yield WaitForSeconds(5);
    3. kabum=false;}
     
  5. Polymorphik

    Polymorphik

    Joined:
    Jul 25, 2014
    Posts:
    599
    I am not familiar with UnityScript but, in C# you have to use StartCoroutine(wait()), perhaps you are not starting it so it never gets called. Again, you have it in the Update function so that can be causing you problems too. I suggest you create a method that starts the coroutine and instantiates whatever objects you want to be created there.
     
  6. love_candy

    love_candy

    Joined:
    Aug 19, 2014
    Posts:
    17
    i checked i a few times and it seams like asigning new value works but...yeild waitforsecounds does not..(it seam that it work only once)
    so the result is that i create large number of objects in instant. I tried ti use"StartCoroutine" but no lack

    1. function wait(){
    2. yield WaitForSeconds(5);
    3. kabum=false;}

    i tried also to add return kabum; but it doest work either

    1. unction wait(){
    2. yield WaitForSeconds(5);
    3. kabum=false;
    4. return kabum;}
     
  7. A.Killingbeck

    A.Killingbeck

    Joined:
    Feb 21, 2014
    Posts:
    483
    Code (JavaScript):
    1.  
    2. if(kabum==true)
    3. {wait();
    4. }
    5.  
    6.  
    This is faulty logic, as you will keep calling Wait function while kabum is still true (which will be the case until the first WaitForSeconds ends). Therefore, because you keep starting a new coroutine, your kabum will keep being reset to false with all those other coroutines you started!
     
  8. love_candy

    love_candy

    Joined:
    Aug 19, 2014
    Posts:
    17
    it works now thx:)
    Code (JavaScript):
    1. #pragma strict
    2. private var  kabum:boolean;
    3. var wybuch1 :GameObject;
    4. //var wybuch2 :GameObject;
    5. //var wybuch3 :GameObject;
    6. //public var lista:Array;
    7.  
    8.  
    9.  
    10. var rr:float;
    11.  
    12. function Start () {
    13. kabum=true;
    14. }
    15.  
    16. function   Update () {
    17.  
    18.  
    19.  
    20. if(kabum){
    21. bum();
    22.  
    23. }
    24.  
    25. }
    26.  
    27.        function bum() {
    28.      
    29. kabum=false    ;
    30. var rr = Random.Range(0.1,1.3);
    31.  
    32.  
    33. var starcie1 = Instantiate(wybuch1,transform.position,transform.rotation);
    34.  
    35. yield WaitForSeconds(rr);
    36. kabum=true;}