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

How could I do that without getting error?

//Thats inside a function

            float tempMinPaused;
            if (tempMinPaused == float.IsNaN){
                  tempMinPaused += Time.deltaTime;
            }

//Operator "==" cannot be applied of operands of type 'float' and 'method group'

IsNaN is a method:

float tempMinPaused;
if (float.IsNan(tempMinPaused)) {
    tempMinPaused += Time.deltaTime;
}

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:

float tempMinPaused;
if (tempMinPaused == float.NaN) {
    tempMinPaused += Time.deltaTime;
}
if (f1 != f1) { // This conditional will be true if f1 is NaN.
1 Like

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’

Trows: Use of unassigned local variable ‘tempMinPaused’

I don’t get it, because, globaly declared floats always get set default to 0, and I can’t check for unassigned local variables

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.

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)

It has a lot of sense:
If i make:

            float tempMinPaused = 0;
            tempMinPaused += Time.deltaTime;
            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.

What exactly are you trying to do? Keep track of how much time has passed?

public class Example : MonoBehaviour
{
    private float time;

    private void Update()
    {
        time += Time.deltaTime;
        Debug.Log(time);
    }
}

It’s doable in browser-based JavaScript because JS just pukes stuff to the global scope:

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.

1 Like

As @LiterallyJeff asked, I think the important question is what are you trying to accomplish. This will help us provide a better solution for you.