Search Unity

Button Pressed

Discussion in 'Scripting' started by emergki, Sep 17, 2014.

  1. emergki

    emergki

    Joined:
    Oct 15, 2007
    Posts:
    422
    Hi, is there any way to know if a button is already pressed when I start the scene?

    For example, I have a car with the Handbrake active when I close the current scene, so, when I open the new scene, to have the Handbrake active again, I need to release the Handbrake and engage it again.

    So, I want to, don't need to release and engage the Handbrake again after switching scenes. How can I do it?
     
  2. Glockenbeat

    Glockenbeat

    Joined:
    Apr 24, 2012
    Posts:
    670
    Create a class and attach it to a gameObject. In that class store the current value for the handbrake (bool handbrakeIsActive) which is set once the handbrake is set and released once the handbrake goes off.

    Also in that script just set DontDestroyOnLoad so it will survive scenes.

    Then once a scene is loaded you may set the handbrake status in Start() or OnLevelWasLoaded() or so by getting a reference to the manager class (singleton may be preferred) and getting the status.
     
  3. emergki

    emergki

    Joined:
    Oct 15, 2007
    Posts:
    422
    Hi #Glockenbeat, thank you for your reply...

    But, using it's method, I'll don't know when the player release the button because Unity3D only know a button is released after the button is pressed, so for example:

    If I have the handbrake pressed on the new class and the Handbrake hardware is engaged (it means the button is pressed), when I release the Handbrake hardware, the Unity3D will not know that the handbrake button was pressed, so, the Handbrake variable will still be "True", in this way I'll still need to engage and disengage the Handbrake to have the result I need.
     
  4. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    2,824
    You may wanna try to check Input.GetKey(KeyCode.-your-key-here-) instead of KeyDown and KeyUp. This should even be true if you press the key before loading the scene.

    You gotta do that in Update though.
     
  5. emergki

    emergki

    Joined:
    Oct 15, 2007
    Posts:
    422
    Hi Suddoha, thank you but, it doesn't work too :-(
     
  6. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    2,824
    How do you start the loading process?
    Gui button or another key or any game related condition?

    I just tried it, it didn't work when i used a key to start the next scene. However, if i use a button this works.
     
  7. emergki

    emergki

    Joined:
    Oct 15, 2007
    Posts:
    422
    With a GUI button. And, the handbrake and other things are from Joystick Buttons.
     
  8. rodrigozol

    rodrigozol

    Joined:
    Apr 4, 2009
    Posts:
    29
    I am having the same problem. Any solutions? Tks