properly handling pause functionality with javascript/unityscript?

So within my game I have a clock script, which keeps track of it’s own float based off of Time.time. You can manipulate it’s verion of Time.time,you can pause time, count down time whatever you would like.

There is also a boolean (“if paused”) attached to this clock script.

Up until this point, I’ve been handling this wrong and basically on everything that I need to be pausable I have it get the clock script, and in function Update, check the status on every frame if pause has happened.

pause = Clock.GetComponent(clockScript).paused

if (pause)
{
  yadda yadda yadda...
}

I was talking to someone last night who’s more experienced at code suggest to me to have whatever button or action that pauses the game, to basically do a check of all units currently in game that can be paused and then perform a co routine (function) to pause them, and of course the same to unpause.

So I “think” what he’s suggesting is…

Funtion PauseEverything ()
{
    Object[] objects = FindObjectsOfType (any object I want paused)
    Run a Pause Function that each object that you can pause would have in it's script.
}

This sounds great but how do I get multiple scripts with multiple names to call out the same function? And the other worry I have is that I heard “FindObjects” or “GameObjects.Find…” is a very slow process and should be used sparingly especially during gameplay. I’m not sure if this all makes sense, I’ve only been coding for about 5 months now so I’m still learning lots. Of course any help on clearing this up would be appreciated.

You don’t need to implement your own Time that you can stop.

That’s what timeScale is for. And you don’t need to tell every Object that the game paused, because they will move based on deltaTime, which means they won’t move anymore and on timeScale=0 FixedUpdate isn’t called anymore.

What about wrapping the pausable objects inside an empty gameobject. Then, when the game is paused, by whatever method, you can then use the BroadcastMessage method of your empty gameobject to call the pause method of it’s children.

myEmptyGameObject.BroadcastMessage("Pause", null);

Then, using the PausableObject class, I can register some methods in another script to be called just like they’re the normal Update() method, with the benefit of being pausable.

For example, a script that constantly rotates an object:

using UnityEngine;
using System.Collections;

public class ConstantRotate : MonoBehaviour {
    
    private bool oldIsSleeping = false;
    private Vector3 oldVelocity;
    private Vector3 oldAngularMomentum;
    
    public void Start() {
        //Register some methods from this script to be called by PauseController
        //IMPORTANT: Do NOT use Update(), FixedUpdate(), or any of the other 
        //default update methods... If you do, during each frame, they'll get 
        //called twice - once by PauseController, then once by Unity! Write your
        //own method names, but other than that pretend they're the same as 
        //the default update methods.
        PauseController.RegisterUpdate(MyUpdate,OnPause,OnUnpause);
        
        //For a script without a rigidbody attached to its GameObject, you can 
        //just use:
        //PauseController.RegisterUpdate(MyUpdate);
    }
    
    public void OnPause() {
        if (!rigidbody) return;
        if (rigidbody.IsSleeping()) oldIsSleeping = true;
        else {
            oldVelocity = rigidbody.velocity;
            oldAngularMomentum = rigidbody.angularVelocity;
            rigidbody.Sleep();
            oldIsSleeping = false;
        }
    }
    
    public void OnUnpause() {
        if (!rigidbody) return;
        if (!oldIsSleeping) {
            rigidbody.WakeUp();
            rigidbody.velocity = oldVelocity;
            rigidbody.angularVelocity = oldAngularMomentum;
        }
    }
    
    public void MyUpdate() {
        transform.rotation = transform.rotation * Quaternion.Euler(0f,10f*Time.deltaTime,0f);
    }
}

Note that rigidbodies need special treatment during pausing / unpausing. This is something I hope the Unity devs will fix in the future, but for now this workaround using OnPause / OnUnpause methods works just fine (as long as nothing bumps into the rigidbody during the pause, of course - that’d make it wake up, and potentially start moving again… so to handle that, you’d need to destroy the rigidbody during OnPause then re-create it during OnUnpause with all its original properties saved and re-loaded).