Timer?

Hey guys,

I don’t know if you read my last thread about referencing another class, but I also forgot to mention there was a problem with the timer. I can;t get it to say anything other than 0 seconds! Here is the code:

moveBullet:

#pragma strict

var bullet: GameObject;
var force = 0.3;
var reloadTime = 0.05;
var lastShotTime = 0;
var isDead: boolean;
var timeTillSwitch = 3.0;
var fixedTime = 0.0;
var startTime: float;
var endTime: float;
var survivedTime: float;

function Start() {

	startTime = Time.fixedTime;

}

function Update() {

	this.transform.Translate(Vector3.forward * force);
	var target = GameObject.Find("Rocket 1");
	if(Vector3.Distance(target.transform.position, this.transform.position) < 2) {
	
		Destroy(target.gameObject);
		Destroy(this);
		isDead = true;
		endTime = Time.fixedTime;
		survivedTime = endTime - startTime;
		
		if(isDead == true) {
		
			Application.LoadLevel("gameoverreplay");
		
		}
		
	}

}

gameovermenu:

#pragma strict

var survivedTime: float;
var timeFinder: moveBullet;

function Start () {

	survivedTime = timeFinder.survivedTime;

}

function Update () {

}

function OnGUI() {

	GUI.Box(Rect(580, 250, 150, 100), "You Died! Play Again?");
	GUI.Box(Rect(580, 275, 150, 100), "You survived " + survivedTime + " seconds.");
	
	if(GUI.Button(Rect(580, 350, 150, 100), "Play Again")) {
	
		Application.LoadLevel("avoid");
	
	}
	
		if(GUI.Button(Rect(580, 475, 150, 100), "Main Menu")) {
	
		Application.LoadLevel("Menu");
	
	}

}

The gameovermenu script is supposed to reference the value of survivedTime from moveBullet, which is calculated by taking the starting time of the game and then subtracting it from another variable, endTime (which is found when the player is killed), so that we can figure their difference.

I can;t figure out what is wrong! Can someone help? Am I referencing it wrong?

Please help!

-Silent

I may not be understanding what you are doing, but is gameovermenu in another scene file than the moveBullet gameobject? If so, Application.LoadLevel will destroy moveBullet. But I would think you’d be getting a null reference error when you try to access timefinder.survivedtime.

If they’re in the same scene, you are setting the value of survivedtime in gameovermenu before you calculate it in the bullet because you assign it in Start so its always 0. Just change your code in gameovernenu where you display the time to the player to use timeFinder.survivedTime and it should get the current value.