FunctionCall from UIButton and Function Call by code give different results, use different spaces?

This is something very strange, maybe I’m missing something because I’ve been coding since past 8 hours, but, simply put, this function:

public int avatarCategoryIndex = 0;
public void PrintCheck()
{
            avatarCategoryIndex++;
            Debug.Log("New Value = " + avatarCategoryIndex);
}

private void Update()
{
            if (Input.GetKeyDown(KeyCode.K))
                PrintCheck();
}

If the function PrintCheck() is called from Update() (by pressing K), then everything works fine, the value is printed as well as the value is visible in the inspector, next to the variable name.

But, if the function PrintCheck() is called by a UI Button, the variable avatarCategoryIndex is incremented and printed, but THAT variable is somehow a separate variable. It is not increasing in the inspector. And the count doesn’t continue from the one in the inspector. But a count is maintained. Subsuquent presses of that UI button show that the value is being stored somewhere as 1,2,3…

It seems there’s separate copies of this class stored in memory? How is this happening I am unable to understand.

It looks like you have 2 instances of the component in the world. Did you also add one to the UIButton?

1 Like

Yes, Indeed :smile: Thank You Sir

1 Like