Have i lost my mind, simple script nor working

why is this simple if stack not working, it is not executing past 1, i tried if else also and still not working, the debug is only showing 0 in the console, never 1,2 etc even though the counter is increasing?

is it just me?

using System.Collections;
using System.Collections.Generic;
using UnityEngine;


public class debugtest : MonoBehaviour {




    public float counter;
   
   
    void Update ()
    {



        if (Input.GetKey(KeyCode.Mouse0)){
  
                        counter=counter+0.02f;

         }






        if (counter<1.0f)
        {

            Debug.Log("0");


            if (counter>1.0f)
            {
       
                Debug.Log("1");
            }
   

            if (counter>2.0f)
            {
   
                Debug.Log("2");
            }
   

            if (counter>3.0f)
            {

                Debug.Log("3");
            }

        }
    }
}

You’ve got SO MANY Debug.Log() statements in there and yet you have failed to print the ACTUAL quantity you care about!!

Why not print the value of counter each frame!? It would instantly reveal what is happening. Instantly.

what do you mean, the counter value is a public variable displayed in the inspector of the script while it is running, so i can see that the counter value is increasing, how do i have SO many debugs, 1 per value?

And the value on the inspector is showing the way it should?

Try a technique called Rubber Duck Debugging. All you do is speak it out through your head, what is the code going to do?

Lets try it.

So if we are pressing Mouse0 this frame then we are increasing the counter by 0.02, then, we check if the counter is less than 1.0, if it is then we are printing a debug, once we have figured out that the counter is less than 1 then we check, within the same if statement, if the same counter value is greater than 1, 2, or 3.

Now just read that and tell me what’s wrong.

1 Like

just noticed no end brace on the first if statement/ what an idiot!!!

Are you sure about that?

my bad

1 Like

I also noticed the value of counter is never initialized, which could lead to all kinds of random and buggy results. You can initialize it as you declare it, like this…

public float counter = 0f;

Not correct. C# does not allow undefined variables. That variable is a class instance variable and is therefore defined to be initialized to default(T) when the class is constructed.

For a float this means it will be 0.0f. This is enforced by the language.

Be careful with field initializers! Because the variable is public / serialized, field initializers can actually lead to more confusing results in Unity. Here’s why:

Field initializers versus using Reset() function and Unity serialization:

https://discussions.unity.com/t/829681/2

https://discussions.unity.com/t/846251/8

Serialized properties in Unity are initialized as a cascade of possible values:

  • what the class constructor makes (field initializers or else default(T) )

  • what is saved with the prefab

  • what is saved with the prefab override(s)/variant(s)

  • what is saved in the scene and not applied to the prefab

  • what is changed in Awake(), Start(), or even later etc.

So you want to make sure you only initialize things at ONE of the above levels, or if necessary, at levels that you specifically understand in your use case. Otherwise errors will seem very mysterious.

2 Likes

Right, that’s one of the great things about C# compared to C++ or C :slight_smile: It’s really hard to shoot yourself in the foot. It’s still possible, but not that easy and in most cases would involve unsafe code and doing kinda stupid things.

1 Like