- Home /
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.
Answer by iwaldrop · Dec 18, 2013 at 07:09 AM
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.
As an example, here's a quick and dirty clock that should allow you easy access to the clock's info. It should work with your JavaScript stuff if you set it higher in the the script compilation order, but it's really just an example. You can get, and store, a reference to it anywhere you want, but you can also access it's values without a reference.
using UnityEngine;
public class Clock : $$anonymous$$onoBehaviour
{
public float timeScale;
public float time;
private static Clock instance;
public static Clock Instance
{
get
{
if (instance == null)
{
instance = GameObject.FindObjectOfType<Clock>();
GameObject clockGO = new GameObject("clock", typeof(Clock));
instance = clockGO.GetComponent<Clock>();
}
return instance;
}
}
public static float Time
{
get
{
return Instance.time;
}
}
public static float TimeScale
{
get
{
return Instance.timeScale;
}
set
{
Instance.timeScale = value;
}
}
void Update()
{
time += UnityEngine.Time.deltaTime * timeScale;
}
}
Hey thanks for taking the time out to help me norm! ( pun intended) my entire pause method is probably a little sloppy. I'm an artist at heart and only started coding 5 months ago.
I was curious if you could more thoroughly explain what you mean by setting a script higher in the compilation order and why that makes it easier for other scripts to access it?
Definitely. The docs are here. Essentially, in order for a UnityScript file to access variables in a C# file, you need to ensure that the C# stuff is in a particular place in your project. This works both ways, however, hence why I referenced the Script Compile Order page that Unity provides. Let me know if you need any additional help.
One thing I left out of the example script above is deltaTime. Which can be gotten by tracking the last time, and returning the difference of the last time and the current time.
Your answer