Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by TemplateR · Sep 20, 2014 at 10:18 AM · buttonpauseswitchresumestatements

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?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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 ++;
     }
 
Comment
Add comment · Show 8 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image TemplateR · Sep 20, 2014 at 11:26 AM 0
Share

I´m writing the game in JavaScript.

avatar image Dhiru · Sep 20, 2014 at 12:06 PM 0
Share

Ok! no problem, Here is the logic behind this.

  1. At the time level load define variable as int (touchCount) type and save its state in playerPrefab.

  2. Detect click or touch on the button.

  3. At each click/touch increase value of touchCount.

  4. Get the value of touchCount % 2.

  5. If value is 1, setTime.TimeScale = 1,(Play the game) and save the value of touchCount in PlayerPrefab.

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

avatar image TemplateR · Sep 20, 2014 at 12:47 PM 0
Share

I think, that you haven´t read my questions:

  1. Is it possible to use switch stetements for this?

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

avatar image Dhiru · Sep 20, 2014 at 01:04 PM 0
Share

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.

avatar image Mr.T · Sep 20, 2014 at 01:16 PM 0
Share

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
     }
 }
 
Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges