Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

[solved ]While loop freezing?

Discussion in 'Scripting' started by Rom210, May 6, 2015.

  1. Rom210

    Rom210

    Joined:
    May 6, 2015
    Posts:
    10
    every time I run:
    Code (CSharp):
    1. function Update () {
    2. //Thing1
    3.     var Thing1 = Random.Range(1,5);
    4.  
    5.  
    6. //turret2
    7.     var Thing2 = Random.Range(1,5);
    8.  
    9. while (Thing2 == Thing){
    10.     Random.Range(1,5);
    11.      }
    12. Debug.Log ("Thing1: " + Thing1 +     "Thing2: " + Thing2);
    13. }
    unity freezes. I read that unity freezes from infinite loops. But I cant find why this is infinite? can anyone help?

    Also if your feeling generous please check out my other question at: http://forum.unity3d.com/threads/destroying-and-not-destroying.323981/
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,798
    Inside your while loop you do not assign the random value to Thing2 or Thing1, so if they are the same, you will endlessly call Random.Range() and discard the results.
     
    Rom210 likes this.
  3. Rom210

    Rom210

    Joined:
    May 6, 2015
    Posts:
    10
    ... I feel dumb. Thanks haha :) noob questions +1
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,798
    No problem. Programmer for 35 years now and I think it has only been a day or so since I've made an error exactly like this.

    Welcome to programming, the final frontier.
     
    A_Marraff and Rom210 like this.