Trying to pause a script...

Hey guys!

So I’ve created a pause function for my game within a clock script. And now I have every object or asset that I want paused outside of the Time.Scale feature to access this script.

Everything I’ve wanted to pause so far, will pause…except this script. I for the life of me cannot get this sucker to pause and resume on command.

#pragma strict

import SmoothMoves;

var resourceValue:			int 	= 	25;

var Countdown:				int 	= 	5;
var visible: 				float 	= 	0.50;
var invisible:				float 	= 	0.50;
var blinkFor: 				float	= 	5.0;

var myAnimation: 			SmoothMoves.BoneAnimation;

var getPlayTime:    	float 	= 	0.0;
private var spawn:				boolean = 	true;
private var startState: 		boolean =	true;
private var startBlink:			boolean = 	true;
var getPlayTimeEnabled: boolean = 	true;
private var getClock:			GameObject;
var whenAreWeDone:		float 	= 	0.0;

function Start () 
{	
	getClock = GameObject.FindGameObjectWithTag("clock");	
	
}

function Update ()
{
	getPlayTime = getClock.GetComponent(clock).playTime;
	getPlayTimeEnabled = getClock.GetComponent(clock).playTimeEnabled;
	
	
	if (getPlayTimeEnabled)
	{
		myAnimation["Idle"].speed 		= 1;
		myAnimation["Spawn"].speed 		= 1;
		
		//renderer.enabled = true;
	
	}
	else
	{
		myAnimation["Idle"].speed 		= 0;
		myAnimation["Spawn"].speed 		= 0;
		//renderer.enabled = false;
		
	}
		
	if (startBlink && getPlayTimeEnabled)
	{
		Blink ();
		startBlink = false;
	}
	
}

function Spawn ()
{
	animation.Play("Spawn");
	yield WaitForSeconds (1);
	startBlink = true;
}

function Blink ()
{
	whenAreWeDone = getPlayTime + blinkFor;
	
	animation.Play("Spawn");
	yield WaitForSeconds (1);
	animation.Play("Idle");
	renderer.enabled = startState;
    yield WaitForSeconds(Countdown);
    
    if (getPlayTimeEnabled)
    {
    	while (getPlayTime < whenAreWeDone)
    	{
        	if ( startState )
        	{
        		renderer.enabled = false;
            	yield WaitForSeconds(invisible);
            	renderer.enabled = true;
            	yield WaitForSeconds(visible);
        	} 
        	else
        	{
        		print ("in else");
            	renderer.enabled = true;
           	 	yield WaitForSeconds(visible);
            	renderer.enabled = false;
            	yield WaitForSeconds(invisible);
        	}
    	}
    
    renderer.enabled = true;
    Destroy ( gameObject );
   	}
}

It’s basically an object that, after it is instantiated, will flash after some time…then destroy itself.

The problem I’m having is, I cannot get it to pause during the flash. (when the code is in the while statement) As you can see I have a global boolean that would detect if the game has been paused, “playTimeEnabled”. And a float that represents the game’s Time.time. “playTime”.

If anyone has any ideas, I’m open to suggestions.

It looks like you should be checking the ‘getPlayTimeEnabled’ flag inside of your while loop, but your variable names are nearly meaningless to anyone but yourself, so it’s hard to say for sure.

You have some weird things going on. Why not just cache a reference to the Clock (or better yet use a singleton pattern on the class or some static methods) instead of using two GetComponent calls each frame per game object that uses this method of getting those variables? That’s what I’d do. For instance:

if (Clock.enabled)
{
    // do something
}

is way better than:

var isClockEnabled : boolean;

void Update()
{
    var clock = getClock.GetComponent(clock);
    isClockEnabled = clock.enabled;
}

void SomeMethod()
{
    if (isClockEnabled)
    {
        // do something
    }
}

Also notice that my variable names are meaningful in and of themselves so that anyone reading the code can figure out exactly what they mean. Self-documenting code is the best kind, my friend.