I’m on step 16 of 17 (Ending the Game) of the Space Shooter tutorials and have run into my first issue with this tutorial series. Not bad considering, but I’d love to know why “Game Over” is prematurely appearing with the destruction of the first asteroid. I have a feeling that it has something to do with the instantiation of the asteroid, but can’t figure it out.
Another thing is that the ‘R’ key is not restarting the game. This isn’t a surprise since the asteroid waves never cease. Which is my last issue; the game won’t end.
I’m bug-eyed from comparing my code with what’s appearing in the tutorial. They seem to be identical … ??
::::::::::GameController Script::::::::::
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;
private bool gameOver;
private bool restart;
private int score;
void Start () {
gameOver = false;
restart = false;
restartText.text = "";
gameOverText.text = "";
score = 0;
UpdateScore ();
StartCoroutine (SpawnWaves ());
}
void Update () {
if (restart) {
if (Input.GetKeyDown (KeyCode.R)) {
Application.LoadLevel (Application.loadedLevel);
}
}
}
IEnumerator SpawnWaves () {
yield return new WaitForSeconds (startWait);
for (int i =0; i < hazardCount; i++) {
while (true) {
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);
if (gameOver) {
restartText.text = "Press 'R' for Restart";
restart = true;
break;
}
}
}
public void AddScore (int newScoreValue) {
score += newScoreValue;
UpdateScore ();
}
void UpdateScore () {
scoreText.text = "Score :: " + score;
}
public void GameOver (){
gameOverText.text = "Game Over!";
gameOver = true;
}
}
::::::::::DestroyByContact::::::::::
using UnityEngine;
using System.Collections;
public class DestroyByContact : MonoBehaviour {
public GameObject explosion;
public GameObject playerExplosion;
public int scoreValue;
private GameController gameController;
void Start () {
GameObject gameControllerObject = GameObject.FindWithTag ("GameController");
if (gameControllerObject != null) {
gameController = gameControllerObject.GetComponent <GameController> ();
}
if (gameController == null) {
Debug.Log ("Cannot find 'GameController' script");
}
}
void OnTriggerEnter(Collider other) {
if (other.tag == "Boundary") {
return;
}
Instantiate (explosion, transform.position, transform.rotation);
if (other.tag == "Player"); {
Instantiate (playerExplosion, other.transform.position, other.transform.rotation);
gameController.GameOver ();
}
gameController.AddScore (scoreValue);
Destroy (other.gameObject);
Destroy (gameObject);
}
}
At some point you have to be able stand alone from the tutorial and apply the lessons learned. Where in this code does GameOver() get called? Find those, backtrack to how it might be called prematurely? For example (not knowing the tut), is this what you want? void OnTriggerEnter(Collider other) { ... if (other.tag == "Player"); { .. gameController.GameOver (); Is the Player shooting something that's perhaps got the tag of Player too?
– getyour411The structure of SpawnWaves() looks odd, can you double check your tut and make sure the for(do) and while(true)... stuff is in the right order?
– getyour411