- Home /
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;
}
}
}
Answer by Wolfram · May 28, 2011 at 06:42 PM
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?
Well, when declaring, I give it the 'true' value. But in the inspector it isn't checked. Check$$anonymous$$eyboardInput()
is called in Update()
. I have however found out that when I check if GUI_ChrosshairActive
is not true, it works...
So the solution is simple: set the checkmark in the inspector, or delete the script from the object and re-attach it.
Answer by aldonaletto · May 28, 2011 at 06:45 PM
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.
That could indeed be the problem.. However, I believe I hadn't done that when I first ran the script. And it wasn't working then either.
Your answer
Follow this Question
Related Questions
Issue with GUI.TextArea keyboard on iPhone 0 Answers
Bind Controller GUI to Input 0 Answers
BCE0004: Ambiguous reference 'Label' 1 Answer
Trigger an event from editor script? 0 Answers
How can I create complex tooltips 2 Answers