Reputation:
I've made a unity3d game and I want to add UI . I've started with a pause button but it doesn't seem to work.
Here is the button info:
I've created an uiManager script to manage the button , as shown in the image above and here is the code :
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class myUIManager : MonoBehaviour {
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
public void Pause() //Function to take care of Pause Button..
{
print("Entered Pause Func");
if (Time.timeScale == 1 && paused == false) //1 means the time is normal so the game is running..
{
print("Enterer first if");
Time.timeScale = 0; //Pause Game..
}
if (Time.timeScale == 0)
{
Time.timeScale = 1; //Resume Game..
}
}
}
Here is the canvas screenshot :
Any ideas? I've been searching for hours ..
Upvotes: 0
Views: 2874
Reputation: 3059
if the condition of your first if
statement is true
then you set your timeScale
to 0
then the condition of the second if
becomes true
then you set it back to 1
You should just change your second if
to an else if
so that if the first condition is true
then your program wont check the second one.
public void Pause()
{
if (Time.timeScale == 1)
{
Time.timeScale = 0;
}
else if (Time.timeScale == 0)
{
Time.timeScale = 1; //Resume Game..
}
}
Upvotes: 1
Reputation: 3113
I think your problem is in your Pause
method:
public void Pause() //Function to take care of Pause Button..
{
print("Entered Pause Func");
if (Time.timeScale == 1 && paused == false) //1 means the time is normal so the game is running..
{
print("Enterer first if");
Time.timeScale = 0; //Pause Game..
}
if (Time.timeScale == 0)
{
Time.timeScale = 1; //Resume Game..
}
}
If you enter the first if
statement you set Time.timeScale = 0
- and then you immediately go into the second if
and set it back to 1.
Try this - it returns
s from the Pause
method once it sets the Time.timeScale
to 0.
public void Pause() //Function to take care of Pause Button..
{
print("Entered Pause Func");
if (Time.timeScale == 1 && paused == false) //1 means the time is normal so the game is running..
{
print("Enterer first if");
Time.timeScale = 0; //Pause Game..
return;
}
if (Time.timeScale == 0)
{
Time.timeScale = 1; //Resume Game..
}
}
If the only two things you want to do in your Pause
method are to set the Time.timeScale
to 0 or 1, you could even simplify it to this:
public void Pause() //Function to take care of Pause Button..
{
print("Entered Pause Func");
if (Time.timeScale == 1 && paused == false) //1 means the time is normal so the game is running..
{
print("Enterer first if");
Time.timeScale = 0; //Pause Game..
}
else
{
Time.timeScale = 1; //Resume Game..
}
}
Upvotes: 1