Search Unity

Resolved KeyPress didn't work

Discussion in 'Scripting' started by qwertypchimmy12, Jan 14, 2022.

  1. qwertypchimmy12

    qwertypchimmy12

    Joined:
    Jan 18, 2021
    Posts:
    23
    Hi Community. I have one problem here. I want to make panel open and close with keypress. But, my script only make the panel close and did not bring it back even when I press the keycode.

    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class OpenClosePanel : MonoBehaviour
    4. {
    5.     public Panel keypanel;
    6.  
    7.     void Start()
    8.     {
    9.  
    10.     }
    11.  
    12.     void Update()
    13.     {
    14.  
    15.         if (Input.GetKeyDown(KeyCode.B))
    16.  
    17.         {
    18.  
    19.             keypanel.gameObject.SetActive(!keypanel.gameObject.activeSelf);
    20.  
    21.             Debug.Log("KeyCode Pressed B");
    22.         }
    23.     }
    24. }
    25.  
     
  2. MaskedMouse

    MaskedMouse

    Joined:
    Jul 8, 2014
    Posts:
    1,092
    Is the OpenClosePanel attached the the keypanel GameObject?
    If so then Update won't be executed after the game object has been disabled.
    Same goes for when the script itself is disabled by using
    enabled = false;
     
  3. qwertypchimmy12

    qwertypchimmy12

    Joined:
    Jan 18, 2021
    Posts:
    23
    Hey @MaskedMouse thankyou for your respond! I have just been able to solve my problem :D I change my code to
    Code (CSharp):
    1.             bool isActive = optionsMenu.activeSelf;
    2.  
    3.             optionsMenu.SetActive(!isActive);