A Unity variable mystery

Im setting up a regular toggle button, like play/pause just Automatic/Manual instead. A boolean value is being "looked at" in two methods. In one method it works in the other the value never registers as true. What could be the problem?


BACKGROUND: I my code i have a simple button, that should work a toggle Button.

When i click the button it set the text of the button to either "Manual" or "Automatic". It will also set a boolean variable to true or false.

So the logic is. On Button click: If boolean variable IsAuto is true(meaning im in automatic mode), then set the button text to "Automatic"(at this point the button should say "Manual", the user will click the set manual mode) and set IsAuto to false. Now we are in manual mode. The button says "Automatic", and the IsAuto is false; If we click again we go back to automatic mode and the button should say "Manual". Just like a play/pause button. This part works. It switches back and forth. Hence my variable works here.

Then another function SetForcedLODGroup is activated. This activation comes from a slider being "slided". OnSliderValueChanged i call SetForcedLODGroup. Now i want to check whether im in manual or automatic "mode", so i look at IsAuto. Here comes the issue. when this method read the IsAuto variable it will always read false.

Maybe a rockie mistake, but take a look at the code. There is no other code touching that variable.

public void OnLODControlConfigurationClick()
{

    var gobj = GameObject.Find("CSL23.STA003");

    if (IsAuto)
    {
        Debug.Log("LOD Boolean is: " + IsAuto);
        gobj.GetComponent<LODGroup>().ForceLOD((int)lodLevel);
        button.GetComponentInChildren<Text>().text = 
        "Automatic";
        obj.text = "Automatic";
        OnForcedLODChangeEvent(lodLevel);

        IsAuto= false;
        Debug.Log("And is changed to: " + IsAuto);

        slider.interactable = true;

    } else {

        Debug.Log("LOD Boolean is: " + booleanLod);
        gobj.GetComponent<LODGroup>().ForceLOD(-1);
        button.GetComponentInChildren<Text>().text = "Manual";
        OnForcedLODChangeEvent(-1);

        IsAuto = true;
        Debug.Log("And is changed to: " + IsAuto);

        slider.interactable = false;

    }

}

.

public int count;

public void SetForcedLODGroup()
{
    Debug.Log("SetForcedLODGroup" + " Count: " + count);
    count++;
    Debug.Log("Boolean Lod value: " + IsAuto);
    if(booleanLod)
    {
        return;
    } else
    {
        var gobj = GameObject.Find("CSL23.STA003");
        gobj.GetComponent<LODGroup>().ForceLOD((int)lodLevel);

        OnForcedLODChangeEvent(lodLevel);
    }
}

The event is just telling a UI label to change its text value.

I expecte that this:

    Debug.Log("SetForcedLODGroup" + " Count: " + count);
    count++;
    Debug.Log("Boolean Lod value: " + IsAuto);

will also return that the boolean value is true after it has been set to true from OnLODControlConfigurationClick(). But it never does. Hopefully someone can explain why this is the case.

0 Answers