setting a default in a keycode variable

hey i did something like this and got the following error:

The !' operator cannot be applied to operand of type UnityEngine.KeyCode’

so whats the proper way of setting up a default key in case no keycode got setup in the inspector?

	public KeyCode UserKey;

	// Use this for initialization
	void Start () {
		
		if(!UserKey){
			
			UserKey = KeyCode.T;
			
		}
	
	}

I think the best way would be to assign a default value directly instead of checking and putting it in Start.

public KeyCode UserKey = KeyCode.T;

ok thanks and yes why not :wink: why make in more complicated then it should be…

An if expression must evaluate to boolean. So you should compare the variable UserKey against of the same type, like another KeyCode in this case…

void Update()
{
  if(UserKey != null  UserKey != KeyCode.T)
  {
    // Do work here.
  }
}

You can use KeyCode.None as a default value if you need to deal with the default case differently.

public KeyCode UserKey = KeyCode.None;

void Start() {
        if (UserKey == KeyCode.None) {
        }
}