Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

GUI.Toggle and Break button

Discussion in 'Scripting' started by Fillock, Sep 10, 2013.

  1. Fillock

    Fillock

    Joined:
    Mar 18, 2009
    Posts:
    73
    Hi all.

    I try to make a Break button; when I touch the button the Time.Scale is set to 0.0, and after a cup of coffee or something I start the game again by touch the button ones more ( set Time.scale back to 1.0 ).

    Only problem it does not work, I have read the reference page and this should not be too complicated, am I missing something?

    Is it better ways to break the game-play by the way? Time.Scale do the job but I see the other buttons are still active and can make some mess even if the time is not-moving.

    Thanks fore any answer.

    Fillock.
     
  2. Digital

    Digital

    Joined:
    Feb 6, 2011
    Posts:
    182
    Time scale is very good to pause your game. Buttons have nothing to do with time scale. You can create a global variable like "pause" and when you hit your Break button change time scale to 0.0 and pause to true. For any button you don't want to work during pause just check if pause is set to false. You can either skip pause variable and check for time scale value like if(Time.timeScale > 0.1) do something or draw buttons that you want to use.
     
  3. Fillock

    Fillock

    Joined:
    Mar 18, 2009
    Posts:
    73
    var myTexture : Texture2D;


    function OnGUI () {

    if (GUI.Button(Rect (Screen.width - 125,15,110,110), myTexture,GUIStyle.none)) {

    Time.timeScale = 0;
    }
    }


    I use this code for now, but it's not what I want since I want to push it one time for pause, and the next time for start the game again. How should I do it right?

    Thanks, that was a good idea, I test for Time.Scale and if it's to slow I disable the other buttons :)
     
  4. SlyRipper

    SlyRipper

    Joined:
    Jun 19, 2012
    Posts:
    251
    Just add a simple if part in there, should do the trick:

    Code (csharp):
    1. if(Time.timeScale == 0)
    2.    Time.timeScale = 1;
    3. else if(Time.timeScale == 1)
    4.    Time.timeScale = 0;
     
  5. Fillock

    Fillock

    Joined:
    Mar 18, 2009
    Posts:
    73
    Hi

    Thanks, I see now it's confusing when I talk about several things in the same tread.

    It's the Button I can't make to work right, I think I have to use GUI.Toggle and not GUI.Button, but I can't get the script to work when I use Toggle