Search Unity

  1. We've closed the job boards. If you're looking for work, or looking to hire check out Unity Connect. You can see more information here.
    Dismiss Notice
  2. We're running great holiday deals on subscriptions, swag and Asset Store packages! Take a peek at this blog for more information!
    Dismiss Notice
  3. Check out our Unite Austin 2017 YouTube playlist to catch up on what you missed. More videos coming soon.
    Dismiss Notice
  4. Unity 2017.2 is now released.
    Dismiss Notice
  5. The Unity Gear Store is here to help you look great at your next meetup, user group or conference. With all new Unity apparel, stickers and more!
    Dismiss Notice
  6. Introducing the Unity Essentials Packs! Find out more.
    Dismiss Notice
  7. Want to see the most recent patch releases? Take a peek at the patch release page.
    Dismiss Notice
  8. Unity 2017.3 beta is now available for download.
    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,583
    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. FMark92

    FMark92

    Joined:
    May 18, 2017
    Posts:
    449
    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,583
    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:
    679
    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:
    2,030
    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,583
    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:
    2,775
    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.