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

I dont understand why this is not working!

Discussion in 'Scripting' started by 1sd15, Dec 16, 2015.

  1. 1sd15

    1sd15

    Joined:
    Dec 15, 2015
    Posts:
    16
    I've recently made a screen pause GUI and I am a noob / new to unity so it is probably a mistake but here is my code and idea for a pause GUI please tell me what you think is wrong :) Note: No errors appear the buttons just don't do anything!

    :
    Code (csharp):
    1.  
    2.  
    3. #pragma strict
    4.  
    5. private var pauseGame;
    6. private var CanvasGUI;
    7.  
    8. function Start ()
    9. {
    10.     if(Input.GetKeyDown("p"))
    11.     {
    12.         pauseGame = true;
    13.         CanvasGUI = true;
    14.     }
    15.  
    16.     if(Input.GetKeyDown("o"))
    17.     {
    18.         pauseGame = false;
    19.         CanvasGUI = false;
    20.     }
    21.  
    22.     if(CanvasGUI == true)
    23.     {
    24.         GameObject.Find("Canvas").GetComponent(Canvas).enabled = true;
    25.     }
    26.  
    27.     if(CanvasGUI == false)
    28.     {
    29.         GameObject.Find("Canvas").GetComponent(Canvas).enabled = false;
    30.     }
    31.  
    32.     if(pauseGame == true)
    33.     {
    34.         Time.timeScale = 0;
    35.         pauseGame = true;
    36. //        GameObject.Find("MainCamera").GetComponent(MouseLook).enabled = false;
    37.     }
    38.  
    39.     if(pauseGame == false)
    40.     {
    41.         Time.timeScale = 1;
    42.         pauseGame = false;
    43. //        GameObject.Find("MainCamera").GetComponent(MouseLook).enabled = true;
    44.     }
    45. }
    46.  
    47.  
     

    Attached Files:

    • Pause.js
      File size:
      767 bytes
      Views:
      614
    Last edited: Dec 16, 2015
  2. LaneFox

    LaneFox

    Joined:
    Jun 29, 2011
    Posts:
    7,381
  3. 1sd15

    1sd15

    Joined:
    Dec 15, 2015
    Posts:
    16
    Ive added the tags - thanks LaneFox for all of your help :)
     
  4. 1sd15

    1sd15

    Joined:
    Dec 15, 2015
    Posts:
    16
    Could you tellme how to make it marked as answered? also when I hit play still nether of the keys do anything! Could it be a bug and be fixed when I build the project? Or am I men't to be it in a object in a certain place ? i just put it in a new empty object as well and it did nothing!
     
    Last edited: Dec 16, 2015
  5. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    this is a forum, not unity answers, so there isn't a "solved".

    start executes once at the beginning of the life of the gameobject. You can't do input checks there. You need to put that within Update().
     
  6. 1sd15

    1sd15

    Joined:
    Dec 15, 2015
    Posts:
    16
    Thanks - as soon as its all done ill delete this - Sorry this is in the wrong section im new!
     
  7. 1sd15

    1sd15

    Joined:
    Dec 15, 2015
    Posts:
    16
    Now when I try it, it is always off! and still dose not turn on! https://gyazo.com/38ac53a3a19323a8007f38e3e65215a6 (Dimmed is when running)

    NewCode(all javascript):
    Code (csharp):
    1.  
    2. #pragma strict
    3.  
    4. private var pauseGame;
    5. private var CanvasGUI;
    6.  
    7. function Update ()
    8. {
    9.     if(KeyCode.Escape)
    10.     {
    11.         pauseGame = true;
    12.         CanvasGUI = true;
    13.     }
    14.  
    15.     if(KeyCode.Delete)
    16.     {
    17.         pauseGame = false;
    18.         CanvasGUI = false;
    19.     }
    20.  
    21.     if(CanvasGUI == true)
    22.     {
    23.         GameObject.Find("Canvas").GetComponent(Canvas).enabled = true;
    24.     }
    25.  
    26.     if(CanvasGUI == false)
    27.     {
    28.         GameObject.Find("Canvas").GetComponent(Canvas).enabled = false;
    29.     }
    30.  
    31.     if(pauseGame == true)
    32.     {
    33.         Time.timeScale = 0;
    34.         pauseGame = true;
    35. //        GameObject.Find("MainCamera").GetComponent(MouseLook).enabled = false;
    36.     }
    37.  
    38.     if(pauseGame == false)
    39.     {
    40.         Time.timeScale = 1;
    41.         pauseGame = false;
    42. //        GameObject.Find("MainCamera").GetComponent(MouseLook).enabled = true;
    43.     }
    44. }
    45.  
    46.  
     
    Last edited: Dec 16, 2015
  8. Fajlworks

    Fajlworks

    Joined:
    Sep 8, 2014
    Posts:
    344
    You have to use:
    Code (CSharp):
    1. if ( Input.GetKeyDown( KeyCode.Escape ) )
    2. {
    3.      pauseGame = true;
    4. }
    Also, you are using more code than it is really needed. You can refactor your code as following and it should work:
    Code (CSharp):
    1. public var myCanvas : Canvas;
    2. private var m_oldPauseValue = false;
    3.  
    4. function Start()
    5. {
    6.      if ( myCanvas == null )
    7.           myCanvas = GameObject.Find("Canvas").GetComponent(Canvas);
    8.  
    9.      if ( myCanvas == null )
    10.      {
    11.           Debug.LogError("Canvas not found! Please provide Canvas component!");
    12.      }
    13. }
    14.  
    15. void OnDestroy()
    16. {
    17.     Time.timeScale = 1f; // in case you reload scene while paused, so you don't have paused game after >_>
    18. }
    19.  
    20. function Update ()
    21. {
    22.     if( Input.GetKeyDown( KeyCode.Escape ) )
    23.     {
    24.         pauseGame = true;
    25.     }
    26.     else if( Input.GetKeyDown( KeyCode.Delete ) )
    27.     {
    28.         pauseGame = false;
    29.     }
    30.  
    31.     if(m_oldPauseValue != pauseGame)
    32.     {
    33.         m_oldPauseValue = pauseGame;
    34.  
    35.         myCanvas.enabled = pauseGame;
    36.         Time.timeScale = pauseGame ? 0f : 1f;
    37.     }
    38. }
    You should avoid calling GameObject.Find() within your Update() method as it is a rather expensive operation. I can assume your canvas will not change, so you can call it once in your Start() method.

    Hope it helps!
     
  9. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    Good catch with the OnDestroy - but why have variables at all (let alone 2 of them)? You could just as easily change the timeScale and enabled directly in the input checks
    Code (csharp):
    1.  
    2. if (Input.GetKeyDown(KeyCode.Escape))
    3. {
    4.     myCanvas.enabled = true;
    5.     Time.timeScale = 0;
    6. }
    7. else if (Input.GetKeyDown(KeyCode.Delete))
    8. {
    9.     myCanvas.enabled = false;
    10.     Time.timeScale = 1;
    11. }
    12.  
     
    Fajlworks likes this.
  10. 1sd15

    1sd15

    Joined:
    Dec 15, 2015
    Posts:
    16
    Thank you loads for the help :) Im still uber noob so Im not sure witch one to pick!
     
  11. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    it's not the wrong place, it's the scripting forum where we talk about and help out with scripting issues within the community and sometimes the devs pop in too. It's just a more "talky" format that then "answers" pages. You also can't delete a thread on the forum, and generally deleting things off a thread in the forum is considered bad form because it breaks the flow of the thread for anyone who stumbles onto the thread at a later date trying to find answers to their issues.
     
  12. 1sd15

    1sd15

    Joined:
    Dec 15, 2015
    Posts:
    16
    FajlWorks has a cool solution but so dose KelsoMRK - I understand KelsoMRKs one nicely yet I like a challenges and I dont know if i should update or start KelsoMRKs one
     
  13. 1sd15

    1sd15

    Joined:
    Dec 15, 2015
    Posts:
    16
    True but im sure im not the only one who wants a pause menu using canvas (Hope im not) so this maybe useful for others if they stumble on it? :p
     
  14. 1sd15

    1sd15

    Joined:
    Dec 15, 2015
    Posts:
    16
    Tried and didn't work :p It may of been the way I placed it etc or I didn't alter all that i should off - any help with that? still noob*
     
  15. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    "Didn't work" is never an adequate description of the problem :)
     
    LeftyRighty likes this.
  16. 1sd15

    1sd15

    Joined:
    Dec 15, 2015
    Posts:
    16
    It said couldent find Canvas then when I tried to fix it it said enabled was not a option! again most likely a noob error
     
    Last edited: Dec 16, 2015
  17. Fajlworks

    Fajlworks

    Joined:
    Sep 8, 2014
    Posts:
    344
    Try copy pasting this code exactly:
    Code (CSharp):
    1. public var myCanvas : Canvas;
    2. function Start()
    3. {
    4.      if ( myCanvas == null )
    5.           myCanvas = GameObject.Find("Canvas").GetComponent(Canvas);
    6.      if ( myCanvas == null )
    7.      {
    8.           Debug.LogError("Canvas not found! Please provide Canvas component!");
    9.      }
    10. }
    11. void OnDestroy()
    12. {
    13.     Time.timeScale = 1f; // in case you reload scene while paused, so you don't have paused game after >_>
    14. }
    15. function Update ()
    16. {
    17.    if (Input.GetKeyDown(KeyCode.Escape))
    18.    {
    19.        myCanvas.enabled = true;
    20.        Time.timeScale = 0;
    21.    }
    22.    else if (Input.GetKeyDown(KeyCode.Delete))
    23.    {
    24.        myCanvas.enabled = false;
    25.       Time.timeScale = 1;
    26.    }
    27. }
    The snippet @KelsoMRK posted was supposed to be your Update() method.

    I would highly recommend downloading some of the Unity projects in the Unity Asset Store. You can learn much by analysing how their code is made.

    Best of luck!
     
  18. 1sd15

    1sd15

    Joined:
    Dec 15, 2015
    Posts:
    16
    Thanks all of you! :D Im sure I will come back with some more errors if I get them :) Thanks again /\_/\
     
  19. 1sd15

    1sd15

    Joined:
    Dec 15, 2015
    Posts:
    16
    Thanks again one thing tho - Void says it is not working ill paste in the command output here:
    Code (csharp):
    1.  
    2. "void" is not a valid macro.
    3.  
     
  20. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    He mixed C# and JS. It should be function OnDestroy
     
  21. 1sd15

    1sd15

    Joined:
    Dec 15, 2015
    Posts:
    16
    Thanks loads again :)
     
  22. 1sd15

    1sd15

    Joined:
    Dec 15, 2015
    Posts:
    16
    Insted of making anohter forum ima just keep putting my issues here :p
     
  23. 1sd15

    1sd15

    Joined:
    Dec 15, 2015
    Posts:
    16
    Moved the new Qs to unity answers
     
    Last edited: Dec 30, 2015