how to make a pause botton clicked by mouse?

Hi

i want to make a button on the screen when i press it it pause the game , and if i press it again it resumes the game, any help guys ? :frowning:

this is my code but it didn’t work :frowning:
1.using UnityEngine;

2.using System.Collections;

4.public class Pause : MonoBehaviour {

  1. void Update() {

  2. if (Input.GetMouseButton (1)) {

  3. if (Time.timeScale == 1.0F)

  4. Time.timeScale = 0.7F;

  5. else

  6. Time.timeScale = 1.0F;

  7. Time.fixedDeltaTime = 0.02F * Time.timeScale;

  8. }

  9. }

15.}

In the future, code tags will make your questions a lot easier to address.

http://forum.unity3d.com/threads/143875-Using-code-tags-properly

You should start with a GUI button.

some basic code/psuedocode to get you started

if (GUI.Button(Rect(10,70,50,30),"pause")){
    if(Paused){
        Paused = false;
        //reset your timescale, plus any other unpause functions that need to happen.
    }
    else{
        Paused = true;
        //set your timescale, plus any other pause functions that need to happen.
    }
}

That should go in your OnGui() function

Try this, which uses OnMouseDown:

using UnityEngine;
using System.Collections;
     
public class Pause : MonoBehaviour {
    
    void OnMouseDown() {
        Time.timeScale = Mathf.Approximately(Time.timeScale, 0f) ? 1f : 0f;
    }
}

It also uses Mathf.Approximately because it’s usually a bad idea to try to compare equality of floating point values.

ok the codes work but I assigned sound clip in the main menu which still playing and not paused, how should I make it pause as well ?

please any help ?

Click on the sound clip in your project view and make sure it isn’t set to “Loop”, also make sure it isn’t set to “Play On Awake”. Loop will make it play forever, and Play On Awake will make it play as soon as the game starts. Both of which you do not want to happen.

^ What he said. :slight_smile:

Also, if you want to turn off the sound when you pause, trying something like this:

    using UnityEngine;
    using System.Collections;

    public class Pause : MonoBehaviour {

        // Assign your main menu audio to this property:
        public AudioSource audioSource = null;
       
        void OnMouseDown() {
            Time.timeScale = Mathf.Approximately(Time.timeScale, 0f) ? 1f : 0f;
            if (audioSource != null) audioSource.Stop();
        }
    }

I have assigned the audio clip to the main menu , so if I unchecked play on awake it wont play , and I also want to be looped , so every time the audio finish it starts again , and I assigned the main menu to the public audio source , the pause work , but when I press it again , the game continue work but the audio keep stopping , so what should I do ?

I made it work :slight_smile:

public class icon : MonoBehaviour {
	bool pause=false;
	public AudioSource audioSource = null;
	void OnMouseDown() {
		if (audioSource != null) { 
			if(pause==true){
				pause=false;
			}
			else{
				pause=true;
			}

		if (pause == true) {
			Time.timeScale=0.0f;
			audioSource.Pause ();
		}
			else
			{
				Time.timeScale=1.0f;
				audioSource.Play();

			}


						
				}


	}
}

Congratulations! I’m glad it’s working. :slight_smile: