Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Toggle GameObject on key?

Discussion in 'Scripting' started by radler470, Jan 14, 2015.

  1. radler470

    radler470

    Joined:
    Dec 3, 2014
    Posts:
    86
    Is there a way (I'm sure there is), to display a GameObject (or UI) when the user hits a specific key? (Like displaying, and then hiding a pop-up of instructions during gameplay)

    How would I go about doing that?

    Much appreciated.
     
  2. achristianini

    achristianini

    Joined:
    Jan 14, 2015
    Posts:
    2
  3. radler470

    radler470

    Joined:
    Dec 3, 2014
    Posts:
    86
  4. SevenHams

    SevenHams

    Joined:
    Dec 27, 2012
    Posts:
    67
    Use this stuff: http://docs.unity3d.com/ScriptReference/Input.html

    Create a reference to the UI you're turning on and off and drag it in in the editor.
    Code (CSharp):
    1. public Canvas onoffCanvas;
    2. bool CanvasIsOn = true;
    3.  
    4. void Update()
    5.  
    6. {
    7.  
    8. if (Input.GetKeyDown("key"))
    9. {
    10.  
    11. if (CanvasIsOn == true)
    12. {
    13. CanvasIsOn = false;
    14. onoffCanvas.enabled = CanvasIsOn;
    15. }
    16.  
    17. else
    18. {
    19. CanvasIsOn = true;
    20. onoffCanvas.enabled = CanvasIsOn
    21. }
    22. }
    23. }
    Doesn't need to be a canvas it can be just like whatever.
     
    Chubtoad5 and achristianini like this.
  5. radler470

    radler470

    Joined:
    Dec 3, 2014
    Posts:
    86
    Worked perfectly, thank you!
     
    achristianini likes this.