- Home /
Pause-Resume one button using switch statements?
Hello,
at the moment I´m programming a simple "SideScroller"-Game. Collecting Points, Timer and Restarting Level. All these things were done alread.
Now I´m wanted to have a "Levelmenu", where you can pause the game or go back to the main-menu. Well one button for "Main-Menu", one button for "Pause" and one button for "resume", thats easy to done using switch statements.
But in the end, I don´t like these menu. Pausing and Resuming a game with 2 buttons? Thats not good in my opinion.
So in the end in my Level-Meu should have these following buttons:
"Main Menu" -"Resume" and "Pause" as one button
By clicking e.g. "Resume", the game resumes and the button "Pause" comes. For clicking "Pause" the other way. I´m using strings for the names of the buttons
Is it possible to use switch stetements for this? If yes, how can I do it? Has someone a example for that?
Answer by Dhiru · Sep 20, 2014 at 11:15 AM
private int pauseCount;
// Use this for initialization
void Start () {
pauseCount = PlayerPrefs.GetInt("Previous game state"); //At The time of loading set pauseCount = 1 as PlayerPrefab
}
// Update is called once per frame
void Update () {
if (pauseCount % 2 == 1) {
// Here play the Game
}
else{
// Here Pause the Game
}
}
//Detecting Mouse click
void OnMouseUp(){
pauseCount ++;
}
Ok! no problem, Here is the logic behind this.
At the time level load define variable as int (touchCount) type and save its state in playerPrefab.
Detect click or touch on the button.
At each click/touch increase value of touchCount.
Get the value of touchCount % 2.
If value is 1, setTime.TimeScale = 1,(Play the game) and save the value of touchCount in PlayerPrefab.
Else value is 0, set timt.timeScale = 0, (Pause the game) and save the value of touchCount in PlayerPrefab.
you skip touchCount value to store in playerPrefab. But i suggest you to store.
I think, that you haven´t read my questions:
Is it possible to use switch stetements for this?
how can you do that? (if the answer of the first question is yes)
I made a Level$$anonymous$$enu with 3 Buttons using the switch-statements. And I wanted, if it possible to keep it.
You can write a separate script for your $$anonymous$$enu.
If you want to use Switch statement then you have to increment the touchCount in the case of play/pause button click. you have to check touchCount value inside update & outside of switch statement. Do the rest of this as you want based on conditions.
I don't think the code below is a good way to code but @TemplateR , Are you looking for something like this?
using UnityEngine;
using System.Collections;
public class SwitchPauseResume : $$anonymous$$onoBehaviour
{
public string buttonToken = "Resumed";
public const string pausedToken = "Paused";
public const string ResumedToken = "Resumed";
void OnGUI()
{
switch (buttonToken)
{
//If the Game is Paused show the Resume Button
case pausedToken:
{
if (GUI.Button(new Rect(20, 20, 100, 30), "Resume"))
{
buttonToken = ResumedToken;
ResumeFunction();
}
break;
}
//If the Game is Not Paused show the Pause Button
case ResumedToken:
{
if (GUI.Button(new Rect (20, 20, 100, 30), "Pause"))
{
buttonToken = pausedToken;
PauseFunction();
}
break;
}
default:
{
break;
}
}
}
void PauseFunction()
{
//Your Pause Code goes here
}
void ResumeFunction()
{
//Your Resume Code goes here
}
}
Your answer
Follow this Question
Related Questions
OnClick button does not work? Trying to make a pause menu 2 Answers
how to pause and resume music after pausing game? 4 Answers
Pause, Unpause 1 Answer
GUI Buttons not working after scene transition 0 Answers
End/Pause Game! 2 Answers