Why doesn't this code work?

I don’t understand why this code doesn’t work.

public KeyCode interactKey = KeyCode.E;

void OnCollisionEnter() {
if (Input.GetKeyDown(interactKey)){
Debug.Log(“Interact”);
meshrenderer.enabled = false;
treeCollider.enabled = false;
}
}

OnCollisionEnter happens for a single physics step. Very unlikely that you’ll press the right key for that exact moment.

Perhaps try OnCollisionStay instead.

I will try that.

It worked! Thanks!

1 Like