I dont understand why this is not working!

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 :slight_smile: Note: No errors appear the buttons just don’t do anything!

:

#pragma strict

private var pauseGame;
private var CanvasGUI;

function Start ()
{
    if(Input.GetKeyDown("p"))
    {
        pauseGame = true;
        CanvasGUI = true;
    }

    if(Input.GetKeyDown("o"))
    {
        pauseGame = false;
        CanvasGUI = false;
    }

    if(CanvasGUI == true)
    {
        GameObject.Find("Canvas").GetComponent(Canvas).enabled = true;
    }

    if(CanvasGUI == false)
    {
        GameObject.Find("Canvas").GetComponent(Canvas).enabled = false;
    }

    if(pauseGame == true)
    {
        Time.timeScale = 0;
        pauseGame = true;
//        GameObject.Find("MainCamera").GetComponent(MouseLook).enabled = false;
    }

    if(pauseGame == false)
    {
        Time.timeScale = 1;
        pauseGame = false;
//        GameObject.Find("MainCamera").GetComponent(MouseLook).enabled = true;
    }
}

2425895–166132–Pause.js (767 Bytes)

Use KeyCodes.

Also, when posting code use code tags.

Ive added the tags - thanks LaneFox for all of your help :slight_smile:

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!

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().

Thanks - as soon as its all done ill delete this - Sorry this is in the wrong section im new!

Now when I try it, it is always off! and still dose not turn on! Screen capture - 38ac53a3a19323a8007f38e3e65215a6 - Gyazo (Dimmed is when running)

NewCode(all javascript):

#pragma strict

private var pauseGame;
private var CanvasGUI;

function Update ()
{
    if(KeyCode.Escape)
    {
        pauseGame = true;
        CanvasGUI = true;
    }

    if(KeyCode.Delete)
    {
        pauseGame = false;
        CanvasGUI = false;
    }

    if(CanvasGUI == true)
    {
        GameObject.Find("Canvas").GetComponent(Canvas).enabled = true;
    }

    if(CanvasGUI == false)
    {
        GameObject.Find("Canvas").GetComponent(Canvas).enabled = false;
    }

    if(pauseGame == true)
    {
        Time.timeScale = 0;
        pauseGame = true;
//        GameObject.Find("MainCamera").GetComponent(MouseLook).enabled = false;
    }

    if(pauseGame == false)
    {
        Time.timeScale = 1;
        pauseGame = false;
//        GameObject.Find("MainCamera").GetComponent(MouseLook).enabled = true;
    }
}

You have to use:

if ( Input.GetKeyDown( KeyCode.Escape ) )
{
     pauseGame = true;
}

Also, you are using more code than it is really needed. You can refactor your code as following and it should work:

public var myCanvas : Canvas;
private var m_oldPauseValue = false;

function Start()
{
     if ( myCanvas == null )
          myCanvas = GameObject.Find("Canvas").GetComponent(Canvas);

     if ( myCanvas == null )
     {
          Debug.LogError("Canvas not found! Please provide Canvas component!");
     }
}

void OnDestroy()
{
    Time.timeScale = 1f; // in case you reload scene while paused, so you don't have paused game after >_>
}

function Update ()
{
    if( Input.GetKeyDown( KeyCode.Escape ) )
    {
        pauseGame = true;
    }
    else if( Input.GetKeyDown( KeyCode.Delete ) )
    {
        pauseGame = false;
    }
  
    if(m_oldPauseValue != pauseGame)
    {
        m_oldPauseValue = pauseGame;

        myCanvas.enabled = pauseGame;
        Time.timeScale = pauseGame ? 0f : 1f;
    }
}

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!

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

if (Input.GetKeyDown(KeyCode.Escape))
{
    myCanvas.enabled = true;
    Time.timeScale = 0;
}
else if (Input.GetKeyDown(KeyCode.Delete))
{
    myCanvas.enabled = false;
    Time.timeScale = 1;
}
1 Like

Thank you loads for the help :slight_smile: Im still uber noob so Im not sure witch one to pick!

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.

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

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? :stuck_out_tongue:

Tried and didn’t work :stuck_out_tongue: 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*

“Didn’t work” is never an adequate description of the problem :slight_smile:

1 Like

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

Try copy pasting this code exactly:

public var myCanvas : Canvas;
function Start()
{
     if ( myCanvas == null )
          myCanvas = GameObject.Find("Canvas").GetComponent(Canvas);
     if ( myCanvas == null )
     {
          Debug.LogError("Canvas not found! Please provide Canvas component!");
     }
}
void OnDestroy()
{
    Time.timeScale = 1f; // in case you reload scene while paused, so you don't have paused game after >_>
}
function Update ()
{
   if (Input.GetKeyDown(KeyCode.Escape))
   {
       myCanvas.enabled = true;
       Time.timeScale = 0;
   }
   else if (Input.GetKeyDown(KeyCode.Delete))
   {
       myCanvas.enabled = false;
      Time.timeScale = 1;
   }
}

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!

Thanks all of you! :smile: Im sure I will come back with some more errors if I get them :slight_smile: Thanks again /_/\

Thanks again one thing tho - Void says it is not working ill paste in the command output here:

"void" is not a valid macro.

He mixed C# and JS. It should be function OnDestroy