Search Unity

  1. We are migrating the Unity Forums to Unity Discussions by the end of July. Read our announcement for more information and let us know if you have any questions.
    Dismiss Notice
  2. Dismiss Notice

Bug Why doesn't this code work?

Discussion in 'Input System' started by see1230, May 11, 2024.

  1. see1230

    see1230

    Joined:
    Aug 4, 2023
    Posts:
    3
    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;
    }
    }
     
  2. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    8,413
    OnCollisionEnter
    happens for a single physics step. Very unlikely that you'll press the right key for that exact moment.

    Perhaps try
    OnCollisionStay
    instead.
     
  3. see1230

    see1230

    Joined:
    Aug 4, 2023
    Posts:
    3
    I will try that.
     
  4. see1230

    see1230

    Joined:
    Aug 4, 2023
    Posts:
    3
    It worked! Thanks!
     
    spiney199 likes this.