- Home /
Problem with restarting the scene (js)
Hello everyone!
When the 1st scene starts the timer capsule object starts to shrink while time is running out and when time ends a game over texture appears on the screen and then u go back to main menu. When you click on play the 1st scene loads again but the timer capsule object is still shrank (scale.y=0). I tried to set again my object with the default scale and color on Start() but it doesn't work. :(
How do i set back the capsule object to its first size everytime i load the scene?
The js script called Begin is attached to MainCamera object and it is shown below:
public var capsuleTimer: GameObject;
public static var flagTxtr:boolean = false;
public static var endTimer:int =0;
public var gameOverClip: AudioClip;
public var hasPlayed:boolean=false;
public var disable_cTmr:boolean=false;
function Update(){
if(disable_cTmr == false){
var cTmr= capsuleTimer.Find("capsuleTimer");
cTmr.renderer.material.color = Color( 0/255.0, 205/255.0, 102/255.0, 0);
var timer:int = 200;
var timer_:int;
var TimeSpeed = 50;
timer_ = timer - (Time.time * TimeSpeed);
cTmr.transform.localScale.y = Mathf.Clamp(timer_, 0, 200);
endTimer = timer_;
var light_ = oDLight.Find("Directional light_1");
if(timer_ <= 100)
cTmr.renderer.material.color = Color( 255/255.0, 165/255.0, 0/255.0, 0);
if(timer_ <= 50)
cTmr.renderer.material.color = Color( 238/255.0, 0/255.0, 0/255.0, 0);
if(timer_ == 0){
flagTxtr=true; // make gameover texture visible by setting variable flagTxtr to true
if(hasPlayed == false){
//Debug.Log("Game Over!");
audio.PlayOneShot(gameOverClip);
hasPlayed = true;
}
light_.light.enabled = false;
Invoke("LoadGameOver",10);
}
}
}
function LoadGameOver(){ //go to High score Scene (from there u can move to main menu and then click on play where my game scene loads
Application.LoadLevel ("High_Score");
}
Answer by Spinnernicholas · Dec 06, 2013 at 04:36 PM
You can just replace Time.time with Time.timeSinceLevelLoad. That is the time since the last level(scene) was loaded.
//........
timer_ = timer - (Time.timeSinceLevelLoad * TimeSpeed);
//........
Great! Nether do i... So many surprises that Unity3d lol Thanks so much for your help @Spinnernicholas
Answer by deltamish · Dec 06, 2013 at 04:21 PM
The Reason is Time.time is number seconds passed since start of the game it doesnt change while switching scenes
So to fix it add these codes
var StartTime:float =0;
function Start(){
StartTime = Time.time;
}
function Update(){
//your code
timer_ = timer - (StartTime * TimeSpeed);
//rest of code
}
@deltamish It's not working.
I think on Start() variable StartTime gets value 0 and on Update() it doesn't start to count at all but stays as 0. That means i can't see my capsule object (timer) Shrinks each time.
Thanx for ur time, anyway.
Your answer
Follow this Question
Related Questions
How to find a prefab in another scene? 1 Answer
Javascript Class Update 2 Answers
How do I get a function to recognize a variable in function Update() 1 Answer
Understanding The Undo Function (JavaScript) 2 Answers
Unity error 2 Answers