Issue with displaying GUI elements

Hello,

I’m currently writing a script for my GUI. They all react to keyboard input. Some parts, which should show up immediately, are only displayed after pressing the button, even though my code should work.

public bool GUI_CrosshairsActive = true;

void OnGUI ()
{
    if (GUI_CrosshairsActive)
    GUI.DrawTexture(CrosshairsSize,Crosshairs);
}

private void CheckKeyboardInput()
{		
    if (Input.GetKeyDown(KeyCode.N))
    {
        if (GUI_Crosshairsactive)
        {
            GUI_Crosshairsactive = false;
        }
        else
        {
            GUI_Crosshairsactive = true;
        }
    }
}

Make sure the value for GUI_CrosshairsActive in the Inspector is also “true”. In case you added the initialization to “true” later in your script, the previous default value “false” will have been stored with the object, and the Inspector does not update on itself, even if you change the script afterwards.

If that is not the solution, we would need more information. Especially, who and when is CheckKeyboardInput() being called?

I’m not sure, but I think the problem is the public declaration of GUI_Crosshairsactive - it should be private instead. When you modify a public variable in the Inspector, the variable keeps the new value and completely ignores the initialisation value set in your script. So, if you have ever altered any instance of this variable in the Inspector, it will plague you for the rest of your lives (its and yours). Change it to private and try again.