Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Object doesn't seem to be checking if a button is being held when spawned

Discussion in 'Scripting' started by Corva-Nocta, Mar 22, 2021.

  1. Corva-Nocta

    Corva-Nocta

    Joined:
    Feb 7, 2013
    Posts:
    801
    Hey all, I have an object that is spawned and needs to check if a button is currently being pressed, but it doesn't seem to be working.

    Here is the simple code I have:
    Code (csharp):
    1.  
    2. void Update()
    3.     {
    4.         if (Input.GetButtonDown("Rewind"))
    5.         {
    6.             StartRewind();
    7.         }
    8.         if (Input.GetButtonUp("Rewind"))
    9.         {
    10.             StopRewind();
    11.         }
    12.         if (Input.GetButtonDown("Pause"))
    13.         {
    14.             StartPause();
    15.         }
    16.         if (Input.GetButtonUp("Pause"))
    17.         {
    18.             StopPause();
    19.         }
    20.     }
    However, if I have the "Pause" button held and then spawn the object, it doesn't seem to check for the button being held down. Any ideas?
     
  2. Corva-Nocta

    Corva-Nocta

    Joined:
    Feb 7, 2013
    Posts:
    801
    I even tried putting some Debug.Logs in the If (Input.GetButtonDown("Pause") and it does not show the debug

    Code (csharp):
    1. void Update()
    2.     {
    3.         if (Input.GetButtonDown("Pause"))
    4.         {
    5.             if (isSpawned)
    6.             {
    7.                 Debug.Log("Update");
    8.             }
    9.             StartPause();
    10.         }
    11.     }
    the debug does not fire
     
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,520
    Break apart gathering of the intention (key up/ key down) into boolean variables so you can easily print them out.

    Then use those boolean variables to kick off what you want to do, perhaps alongside Debug.Log() statements.

    If you can't get them all working at once, comment them out and try them one at a time.
     
    Corva-Nocta likes this.
  4. Corva-Nocta

    Corva-Nocta

    Joined:
    Feb 7, 2013
    Posts:
    801
    I think I got it to work, I was checking Input.GetButtonDown which only checks for when the button is first presssed, but it seems that Input.GetButton will check if it is currently being held, so it seems to work for now!