Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

While statement locking up Unity?

Discussion in 'Scripting' started by xacto, Aug 20, 2005.

  1. xacto

    xacto

    Joined:
    Jul 20, 2005
    Posts:
    112
    I have a script that is working fine untill I add a "While" function. Any ideas why this would cause Unity to lock up?

    Thanks!


    Code (csharp):
    1.  
    2. var projectile : Rigidbody;
    3. var chargeTime = -1.0;
    4. var ballCount = 6; // how many balls left
    5. var guiBalls : GUIText; // connect to the guiText
    6.  
    7. function Update () {
    8.     var autoDestroySeconds = 25;
    9.    
    10. while (ballCount  > 1){  // perform until there are no more
    11.  
    12.      if (Input.GetMouseButtonDown(0)){
    13.         chargeTime = Time.time;
    14.    
    15.      }
    16.  
    17.     if (!Input.GetMouseButton (0)  chargeTime != -1.0) {
    18.         var speed = 5 + 10 * (Time.time - chargeTime);
    19.         ray = Camera.mainCamera.ScreenPointToRay (Input.mousePosition);
    20.         var hit : RaycastHit;
    21.  
    22.         if (audio  !audio.isPlaying) {
    23.             audio.Play ();
    24.         }
    25.  
    26.     //display how many balls are left
    27.     ballCount --;
    28.     guiBalls.text = "Balls: " + ballCount.ToString();
    29.  
    30.  
    31.  
    32.         if (Physics.Raycast (ray, hit)) {
    33.             target = hit.point;
    34.         }
    35.         else {
    36.             target = (ray.origin + ray.direction * 100);
    37.         }
    38.        
    39.         direction = target - transform.position;
    40.         var instantiatedProjectile : Rigidbody = Instantiate (projectile, transform.position, Quaternion.FromToRotation (Vector3.fwd, direction));
    41.         instantiatedProjectile.velocity = direction.normalized * speed;
    42.         Destroy (instantiatedProjectile, autoDestroySeconds);
    43.         chargeTime = -1.0;
    44.     }
    45. }
    46.  
    47. }
    48. Debug.Log("THE END");
    49.  
     
  2. Joachim_Ante

    Joachim_Ante

    Unity Technologies

    Joined:
    Mar 16, 2005
    Posts:
    5,203
    The problem is that you never exit the Update function inside your while loop.

    you while until no balls are left, then you check in the while loop if the button is pressed. But since you never exit the while loop, you will never get after the first frame.

    What you want to do is most likely using coroutines.

    So do all of this in the Start function instead of Update function and then at the end of the loop put a yield statement.
    The yield statement, will pause that script and continue where it left off next frame.
     
  3. xacto

    xacto

    Joined:
    Jul 20, 2005
    Posts:
    112
    Thank you... will give it a try!
     
  4. xacto

    xacto

    Joined:
    Jul 20, 2005
    Posts:
    112
    Where is the best place to drag the start function? In the camera?
     
  5. Joachim_Ante

    Joachim_Ante

    Unity Technologies

    Joined:
    Mar 16, 2005
    Posts:
    5,203
    Not sure what you mean.

    I meant that you do the following changes in your script:

    1) Replace function Update ()
    with function Start ()

    2) Add yield to the end of the while loop.
    (before the } which closes the while loop)