Toggle bool statment

I got to be missing something simple here.

I have tried GetKeyDown, GetKeyUp and the like.

I do want to use GetKey if anyone has any suggestions to why this wouldn’t toggle the bool.:slight_smile:

    if(Input.GetKey("o")) { options = !options; 
    Debug.Log("options = !options");
    }

Works fine for me, is this code inside another ‘if’ statement? Is the code inside Update? Just check the basic things, is the script even attached to a game object? If so, is it enabled(C# only)? The code you posted is fine, the problem must be with something else.

GetKey constantly fires, so its probably swapping it 100 times/s or however fast its occuring.

TY for the reply.

Ok, the if statment is not in another if condition (check)

The code is inside a OnGUI so that should work (check)

I’m getting the debug from hitting o, so it’s on a game object (check)

I have never heard of enabled C# only. I have been using unity for a while now and since i use Unity Script.
Where is that option?

All my other code in the OnGUI works. Most the time i can code my way out any solution, but today i can’t even get the basics to work lol.

Don’t check Input in OnGUI as it gets called more than once per frame - only in Update.

I tried GetKeyDown and Up. To avoid setting up input axis i been using GetKey most the time instead of GetButton etc.

It’s just been one o f those days lol.

I beleive the enabled option he is talking about can be seen in the inspector at the top of the script. Chances are this is not the problem.

I would use GetKeyDown in Update… inspect the options variable to ensure it is checking unchecking as expected.

TY That was the problem. i used GetKeyDown In Update.

Why would OnGUI get called more that once? Is it because it will skip a line sometimes.

It is because OnGUI is used by unity for repaint, layout, and other GUI events. Each time an event is thrown OnGUI is called. These events have to do with input, so that might be why Input functions won’t work. See: Unity Docs: Events

Did you check and make sure that for some reason your input keys didn’t get messed up. That happened to me once. I was changing some keys and I changed the wrong one, so it took me a few hours to find the issue.