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!
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!
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.
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
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
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.