Search Unity

while (Input.mouseScrollDelta.y != 0) crashes unity

Discussion in 'Scripting' started by Kyzen_, Sep 12, 2019.

  1. Kyzen_

    Kyzen_

    Joined:
    May 24, 2019
    Posts:
    8
    I have the following code in update:
    Code (CSharp):
    1. while (Input.mouseScrollDelta.y != 0)
    2.         {
    3.             Debug.Log(Input.mouseScrollDelta);
    4.         }
    I also get a crash when I use Input.GetAxis("Mouse ScrollWheel")

    I want to use the while loop to increment/decrement a variable while the scroll wheel is being turned. The Debug.Log is just there to check the sort of values I get.

    When I use an IF statement unity doesn't crash so I'm wondering why using a WHILE statement crashes Unity. I'm new to C# and Unity so apologies if the answer is obvious.
     
  2. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
    It doesn't crash, it freezes. It's an infinite loop. Unity only ever updates input data (or anything, really) between frames, and as long as your code is running, it won't be between frames. So, Input.mouseScrollDelta will never change, and it'll just loop forever.
     
    Joe-Censored, Yoreki and Kyzen_ like this.
  3. Kyzen_

    Kyzen_

    Joined:
    May 24, 2019
    Posts:
    8
    So when I turn the scroll wheel Unity executes the while loop and tries to print Input.mouseScrollDelta an infinite amount of times in a single frame, but since it can't print it an infinite number of times it can't exit the while loop. Am I right in saying that?
     
  4. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
    Yep, that's it.

    Even if there were nothing in the loop, it'd still get stuck, just evaluating Input.mouseScrollDelta.y != 0 over and over and over.
     
  5. Kyzen_

    Kyzen_

    Joined:
    May 24, 2019
    Posts:
    8
    I understand now. Thank you StarManta for the help.
     
  6. Dameon_

    Dameon_

    Joined:
    Apr 11, 2014
    Posts:
    542
    Joe-Censored and Kyzen_ like this.
  7. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
    While a good general rule, that wouldn't be a factor here. Floating point inaccuracy only really comes up when math is applied to the float being compared. If you set a float explicitly to 0.0, it will reliably be compared to 0.0 in the future; and when Input.mouseScrollDelta's value is being determined, it almost certainly has something like:
    Code (csharp):
    1. if (scroll wheel isn't being moved) {
    2. return 0f;
    3. }
    There's no math being done there, so in this case, comparing the result to 0f should be reliable.

    (That is the exception rather than the rule, of course; in most cases this advice is correct.)
     
  8. Kyzen_

    Kyzen_

    Joined:
    May 24, 2019
    Posts:
    8
    Then is it better for me in the future to compare a float to how near it is to 0 and set a variable to decide how sensitive I want it to be rather than compare to 0 whenever I can? So long as there is no math involved as StarManta says?
     
  9. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
    Yeah, Mathf.Approximately exists for this purpose. And for the sake of clarity, it wouldn't hurt to use it even in cases where == would work, it's just not necessary in those cases.
     
  10. Kyzen_

    Kyzen_

    Joined:
    May 24, 2019
    Posts:
    8
    Thank you once again to the both of you for the insight :D