1. Help us improve the editor usability and artist workflows. Join our discussion to provide your feedback.
    Dismiss Notice
  2. We're looking for feedback on Unity Starter Kits! Let us know what you’d like.
    Dismiss Notice
  3. We’re giving 2017.1 beta testers a chance to win t-shirts and a Nintendo Switch. Read more on the blog.
    Dismiss Notice
  4. We want to know how you learned Unity! Help us by taking this quick survey and have a chance at a $25 gift card
    Dismiss Notice
  5. Unity 5.6 is now released.
    Dismiss Notice
  6. Check out all the fixes for 5.6 on the patch releases page.
    Dismiss Notice

Float t == Float.NaN ?¿?¿?¿?¿?¿?¿?¿?

Discussion in 'Scripting' started by qiqette, Jun 19, 2017.

  1. qiqette

    qiqette

    Joined:
    Feb 7, 2016
    Posts:
    121
    How could I do that without getting error?

    Code (CSharp):
    1. //Thats inside a function
    2.  
    3.             float tempMinPaused;
    4.             if (tempMinPaused == float.IsNaN){
    5.                   tempMinPaused += Time.deltaTime;
    6.             }
    7.  
    8. //Operator "==" cannot be applied of operands of type 'float' and 'method group'
     
  2. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    2,170
    IsNaN is a method:

    Code (csharp):
    1. float tempMinPaused;
    2. if (float.IsNan(tempMinPaused)) {
    3.     tempMinPaused += Time.deltaTime;
    4. }
    Although you probably want !float.IsNan. Adding only when the value is NaN seems a bit strange!

    You could also check for equality with Nan:

    Code (csharp):
    1. float tempMinPaused;
    2. if (tempMinPaused == float.NaN) {
    3.     tempMinPaused += Time.deltaTime;
    4. }
     
  3. Marfab

    Marfab

    Joined:
    May 18, 2017
    Posts:
    155
    Code (CSharp):
    1. if (f1 != f1) { // This conditional will be true if f1 is NaN.
     
  4. qiqette

    qiqette

    Joined:
    Feb 7, 2016
    Posts:
    121
    Thank you for the reply, although i get errors in both solutions :S

    First case: Use of unassigned local variable 'tempMinPaused'
    Second case: Use of unassigned local variable 'tempMinPaused'
     
  5. qiqette

    qiqette

    Joined:
    Feb 7, 2016
    Posts:
    121
    Trows: Use of unassigned local variable 'tempMinPaused'
     
  6. qiqette

    qiqette

    Joined:
    Feb 7, 2016
    Posts:
    121
    I don't get it, because, globaly declared floats always get set default to 0, and I can't check for unassigned local variables
     
  7. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    2,170
    I assumed that tempMinPaused was a variable you had from somewhere else.

    What are you trying to do here? Creating a new variable and then checking if it's NaN or not is nonsensical. It won't be, unless you set it to NaN.
     
  8. JoshuaMcKenzie

    JoshuaMcKenzie

    Joined:
    Jun 20, 2015
    Posts:
    566
    Then assign it. give it a default value you want it to start at.

    And no, outside fields can also throw the unassigned exception. usually happens with reference types and certain structs. but simple value types can do this as well

    Edit:
    He could be getting NAN due to a math result such as dividing by 0 or Tan(Pi/2)
     
  9. qiqette

    qiqette

    Joined:
    Feb 7, 2016
    Posts:
    121
    It has a lot of sense:
    If i make:
    Code (CSharp):
    1.             float tempMinPaused = 0;
    2.             tempMinPaused += Time.deltaTime;
    3.             Debug.Log (tempMinPaused);
    I will get the duration of the last frame, every time the function is called.
    If I couldn't declare it, i will just equal it to 0 the first time the function is called, and the rest of times, when it were already declared to 0 sum the duration of every lastFrame.

    It's doable in Javascript and very useful.
     
  10. jeffreyschoch

    jeffreyschoch

    Joined:
    Jan 21, 2015
    Posts:
    1,650
    What exactly are you trying to do? Keep track of how much time has passed?
    Code (CSharp):
    1. public class Example : MonoBehaviour
    2. {
    3.     private float time;
    4.  
    5.     private void Update()
    6.     {
    7.         time += Time.deltaTime;
    8.         Debug.Log(time);
    9.     }
    10. }
     
  11. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    2,170
    It's doable in browser-based JavaScript because JS just pukes stuff to the global scope:

    Code (csharp):
    1. x = 5; //now there's a global x that's 5, congratulations
    "very useful" is an interesting way to put "fundamentally broken". If you now write the same thing in a different script anywhere, the two scripts are now re-defining the same global variable.

    Not sure if UnityScript works like that - I doubt it? That would mean that they'd introduced a global key-value store to UnityScript to match how broken JavaScript is.
     
    jeffreyschoch likes this.
  12. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    1,927
    As @jeffreyschoch asked, I think the important question is what are you trying to accomplish. This will help us provide a better solution for you.