- Home /
How Can I fix my timer in this code?
using UnityEngine; using System.Collections;
public class GameController : MonoBehaviour { public GameObject hazard; //public Vector3 spawnValues; public int hazardCount; public float spawnWait; public float startWait; public float waveWait;
public GUIText scoreText;
public GUIText restartText;
public GUIText gameOverText;
public GUIText TimerText;
public float targetTime = 0.60f;
private bool gameOver;
private bool restart;
private int score;
public Transform[] spawnPoints;
void Start ()
{
gameOver = false;
restart = true;
restartText.text = "Press 'R' for Restart";
gameOverText.text = "";
TimerText.text = "Loser in:"+ targetTime;
score = 0;
UpdateScore ();
UpdateTimer ();
StartCoroutine (SpawnWaves ());
}
void Update ()
{
if (restart)
{
if (Input.GetKeyDown (KeyCode.R))
{
Application.LoadLevel (Application.loadedLevel);
}
if (gameOver)
{
restartText.text = "Press 'R' for Restart";
restart = true;
}
}
}
IEnumerator SpawnWaves ()
{
yield return new WaitForSeconds (startWait);
while (true)
{
for (int i = 0; i < hazardCount; i++)
{
int spawnPointIndex = Random.Range (0, spawnPoints.Length);
Instantiate (hazard, spawnPoints[spawnPointIndex].position, spawnPoints[spawnPointIndex].rotation);
yield return new WaitForSeconds (spawnWait);
}
yield return new WaitForSeconds (waveWait);
}
}
//IEnumerator SpawnWaves ()
//{
//yield return new WaitForSeconds (startWait);
//while (true)
//{
//for (int i = 0; i < hazardCount; i++)
//{
// Vector3 spawnPosition = new Vector3 (Random.Range (-spawnValues.x, spawnValues.x), spawnValues.y, spawnValues.z);
// Quaternion spawnRotation = Quaternion.identity;
// Instantiate (hazard, spawnPosition, spawnRotation);
// yield return new WaitForSeconds (spawnWait);
//}
// yield return new WaitForSeconds (waveWait);
//}
//}
public void AddScore (int newScoreValue)
{
score += newScoreValue;
UpdateScore ();
}
void UpdateScore ()
{
scoreText.text = "Souls " + score;
}
void Timer ()
{
targetTime -= Time.deltaTime;
if (targetTime <= 0.00f)
{
timerEnded();
}
UpdateTimer ();
}
void UpdateTimer()
{
TimerText.text = "Loser in:" + targetTime;
}
void timerEnded()
{
//if (targetTime <= 0.0f) {
GameOver ();
//}
}
public void GameOver ()
{
gameOverText.text = "doomed!";
gameOver = true;
}
}
How is its behaviour differing from the intended behaviour?
I'm a noob but I think
public GUIText TimerText;
should be
public GUIText timerText;
also needs to be changed on line 19 and 107
Answer by kbaloch · Apr 23, 2015 at 01:43 PM
you should call your timer method in start method by using invokerepeating just like this
void start ()
{
// your code
InvokeRepeating("Timer",time,repeatRate); //use your values
}
void Timer ()
{
// your code
if (gameOver) {
CancelInvoke("Timer");
}
}
or just call Timer in update methode but is not good approach
Do I have to delete the rest of the code that is related to the timer?
no need to delete any thing just add these line as i stated
Answer by Sethhalocat · May 11, 2015 at 08:02 PM
I would do a wait for seconds and then load up a quick scene saying game over. Or wIt for seconds and start void game over.