- Home /
Script wait for 3 seconds before running - stops it working altogether
Hi - I want a pause of 3 seconds before this script starts running.
I've tried this method, and although it doesn't run any errors - it stops the script from working altogether? It seems to have worked for other people on the forums, so I was wondering if anyone knew what I'm doing wrong?
private var cLight:GameObject;
var intensitySpeedBlackFirst : float = 0.05;
var intensitySpeedBlackThird : float = 0.05;
var intensitySpeedWhiteFirst : float = 0.02;
var intensitySpeedWhiteThird : float = 0.02;
public static var IsSwitch:boolean = false;
private var IsChange:boolean = false;
var lock = true;
function Start () {
cLight = gameObject.Find("Lighting");
yield WaitForSeconds (3);
lock = false;
}
function Update () {
if(!lock) {
if(IsSwitch) {
if(!IsChange) {
if(SwitchCharacters.check == 0)
cLight.renderer.material.color.a -=Time.deltaTime*intensitySpeedBlackFirst;
else
cLight.renderer.material.color.a -=Time.deltaTime*intensitySpeedBlackThird;
if(cLight.renderer.material.color.a<=0) {
IsChange = true;
cLight.renderer.material.color = Color(0,0,0,0);
}
}
else {
//this is first person controller
if(SwitchCharacters.check == 0)
cLight.renderer.material.color.a +=Time.deltaTime*intensitySpeedBlackFirst;
else
cLight.renderer.material.color.a +=Time.deltaTime*intensitySpeedBlackThird;
if(cLight.renderer.material.color.a>=1) {
//screen goes black
}
}
}
else {
if(!IsChange) {
//this is first person controller
if(SwitchCharacters.check == 0)
cLight.renderer.material.color.a +=Time.deltaTime*intensitySpeedWhiteFirst;
else
cLight.renderer.material.color.a +=Time.deltaTime*intensitySpeedWhiteThird;
if(cLight.renderer.material.color.a>=1) {
//screen goes white
}
}
else {
//this is first person controller
if(SwitchCharacters.check == 0)
cLight.renderer.material.color.a -=Time.deltaTime*intensitySpeedWhiteFirst;
else
cLight.renderer.material.color.a -=Time.deltaTime*intensitySpeedWhiteThird;
if(cLight.renderer.material.color.a<=0) {
IsChange = false;
cLight.renderer.material.color = Color(1,1,1,0);
}
}
}
}
}
Any help would be so much appreciated!
Thanks, Laurien
Answer by laurienash · Sep 17, 2013 at 07:56 PM
Hi - I've just done it in the cheats way by adding this script to a collider:
#pragma strict
var lightController : GameObject;
function Start () {
lightController.active =false;
}
function OnTriggerEnter (other:Collider) {
if(other.tag == "Player") {
lightController.active =false;
yield WaitForSeconds (3);
lightController.active = true;
}
}
Thanks for your help though!
Answer by Yokimato · Sep 17, 2013 at 06:51 PM
I can't find the documentation after looking quickly, but I remember trying to make Start() a coroutine and it not working...but as a work around, you call a coroutine from Start like so:
function Start() {
cLight = gameObject.Find("Lighting");
StartCoroutine(SetLock(false));
}
function SetLock(status: boolean) {
yield WaitForSeconds (3);
lock = status;
}
Hmm - I've tried that, I'm not getting any errors, or anything in the log, but it's not working. If I put if (!lock) {
in the update function nothing happens, and if I comment it out it works immediately (just as before) Any ideas?
Here's the updated script:
var intensitySpeedWhiteFirst : float = 0.02;
var intensitySpeedWhiteThird : float = 0.02;
public static var IsSwitch:boolean = false;
private var IsChange:boolean = false;
var lock = true;
function Start() {
cLight = gameObject.Find("Lighting");
StartCoroutine(SetLock(false));
}
function SetLock(status: boolean) {
yield WaitForSeconds (3);
lock = status;
}
function Update () {
if (!lock) {
if(IsSwitch) {
if(!IsChange) {
if(SwitchCharacters.check == 0)
cLight.renderer.material.color.a -=Time.deltaTime*intensitySpeedBlackFirst;
else
cLight.renderer.material.color.a -=Time.deltaTime*intensitySpeedBlackThird;
if(cLight.renderer.material.color.a<=0) {
IsChange = true;
cLight.renderer.material.color = Color(0,0,0,0);
}
}
else {
//this is first person controller
if(SwitchCharacters.check == 0)
cLight.renderer.material.color.a +=Time.deltaTime*intensitySpeedBlackFirst;
else
cLight.renderer.material.color.a +=Time.deltaTime*intensitySpeedBlackThird;
if(cLight.renderer.material.color.a>=1) {
//screen goes black
}
}
}
else {
if(!IsChange) {
//this is first person controller
if(SwitchCharacters.check == 0)
cLight.renderer.material.color.a +=Time.deltaTime*intensitySpeedWhiteFirst;
else
cLight.renderer.material.color.a +=Time.deltaTime*intensitySpeedWhiteThird;
if(cLight.renderer.material.color.a>=1) {
//screen goes white
}
}
else {
//this is first person controller
if(SwitchCharacters.check == 0)
cLight.renderer.material.color.a -=Time.deltaTime*intensitySpeedWhiteFirst;
else
cLight.renderer.material.color.a -=Time.deltaTime*intensitySpeedWhiteThird;
if(cLight.renderer.material.color.a<=0) {
IsChange = false;
cLight.renderer.material.color = Color(1,1,1,0);
}
}
}
}
}
I'm not a "UnityScript" master, only c#...however, the only thing I noticed as different is how you're initializing lock
. What if you changed: var lock = true;
to: private var lock:boolean = true;