Hi all, 
I have a game with alot of gameObjects and alot of scripts…
I wanted to pause the game when the user press P or Esc:
using UnityEngine;
using System.Collections;
public class PauseMenu : MonoBehaviour {
private bool IsPause;
public int LabelWidth;
// Use this for initialization
void Start () {
if(LabelWidth==0)
LabelWidth=100;
IsPause=false;
}
// Update is called once per frame
void Update () {
if(Input.GetKeyUp(KeyCode.P)||Input.GetKeyUp(KeyCode.Escape))
{
Time.timeScale=1.0f-Time.timeScale;
IsPause=!IsPause;
}
}
void OnGUI(){
if(IsPause)
{
GUI.Box(new Rect(10,10,Screen.width-20,Screen.height-20),"Pause Menu");
GUI.Label(new Rect(Screen.width/2-LabelWidth/2,Screen.height/2,LabelWidth,30),"The Game is paused");
}
}
}
but unfortunately, every gameObject continue to do his script and every rigidbody continue…
I tryed Debug.Break();
but it cant be unbreak in the script…
i tryed many things that supost to pause but each one, do not do his script…
Why Time.timeScale =0 doesnt work? 

please help…
if you have a well structured script hierarchy where you have a single script controlling/updating all of your other scripts, a pause can be rather easy to implement. It comes down to a simple bool check in one or two of your higher tier scripts. If not you may need to put a bool check in everyone one of your scripts and do a mass call to everything from whatever is handling your pause button.
if i enabled all the scripts, after enbaled it again do they start from the function Start() ?
and i cant also delete the rigidbodys… there and too much and delete them will take time…
i think i will have to do the mass Call…
anyone have another idea? :S
One possible solution that wouldn’t require a whole lot of refactoring of existing code.
public class baseObject : MonoBehaviour
{
public virtual void newUpdate();
public bool isPaused = false;
public void Update()
{
if (!isPaused)
newUpdate();
}
}
public class anotherObject : baseObject
{
public override void newUpdate()
{
// do your normal update stuff here
}
}
If I understand this code, that means every GameObject will be connected to the mainObject which on pause wont let anything happen…
that looks great
but only one question…
Does the anotherObject is MonoBehaviour too, i mean, could i put it in another GameObject and the script will work fine on it?
if so, you are genius
D
You can also simply have a global static bool variable checked at the beginning of every one of your Update, LateUpdate, etc method across all your scripts, if false you can simply return from the method. A bit messy since you would have to update all of them with this and make sure any new script includes this, but at least you wouldn’t have to change any class hierarchy. I also have a lot of objects but relatively few scripts running, so this method is enough in my case; nevertheless a centralized hierarchy based solution is clearly better.
Yes. anotherObject → baseObject → MonoBehaviour → on and on. Inheritance is your friend. 
i got an error…
public virtual void NewUpdate();
(MainMenu is the main script)
anything else is working good, I think :)(unity let me see another errors, only after I slove this one)
Sorry. It should be abstract if there is nothing i the method. If you want “default” behavior then make it virtual, put your default code in there, and don’t override the method in your subclass.
Confused my keywords, my bad.
I might didnt understand what you wanted…
I made it :
public abstract void NewUpdate();
but it gives my the error:
I didnt know this Inheritance code before so it’s new to me…
Sorry. I should suck less at explaining concepts. So you have 2 options
public abstract class baseImpl : MonoBehaviour {
public abstract void newUpdate();
}
public class baseClass : baseImpl {
public bool isPaused; // can also be 'protected' instead of public if you want
public void Update()
{
if (!isPaused)
newUpdate();
}
public override void newUpdate()
{
// do stuff
}
}
or
public class baseObject : MonoBehaviour {
public virtual newUpdate()
{
// do default logic here
}
}
public class newObject : baseObject {
public override newUpdate()
{
// base.newUpdate(); <-- call this if you want to do default logic AND additional logic
// do new logic here
}
}
First one just declares a virtual method with no implementation. Implementation is left up to any class deriving from the abstract class.
Second one declares a virtual method with an implementation, so if no matching method is found in a subclass then that one is executed by default. base.newUpdate(); will call the parent class’ method. Good if you want to add to existing logic. If you do that, it must be the first line in the override method.
This might be some interesting reading to get your feet wet.
I read what you gave me and after alot of thinking i did a static IsPause, and on each update it will check if IsPause is true or false:
void Update(){
if(!MainMenu.IsPause){
//doing what i want
}
}