- Home /
Space Shooter problems
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?
Hi, thanks for responding. I agree about kicking off the training wheels so in spite of my entirely novice understanding of code I'll attempt to do so :D
For the easy question: I've checked the Asteroid Prefab and it's component and neither are tagged for anything. Nothing is Tagged as the player except the Player.
As for your first question; I can only find GameOver () in the two scripts I provided in my original question. As far as I can tell the GameController says when the the game is over at the bottom of the page, but I don't think this is ever getting called. The "Game Over!" text appears so I know it's at least reaching the line to call it, but the very next line is suppose to break the while loop that calls the wave of asteroids. I know it's not reaching that part because the asteroids keep co$$anonymous$$g and the text "Press 'R'to restart" never appears.
I discovered that my while loop was goofed so I fixed it but it wasn't the culprit.
I'm still at a complete loss since what little I can gather seems correct to me. I even tried capitalizing the 'G' in GameOver but the game wouldn't even run. I'm baffled.
The 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?
Answer by CyberGolem · Mar 31, 2014 at 05:04 AM
Well, it seems that fixing the while loop earlier did make some progress after all. Everything works now (i.e. 'R' key resets the game, asteroids stop after the Player is killed, etc.,) except that destroying the first asteroid still prematurely invokes the "Game Over! "text.
I've gone line by line against the code in the video and can't find a single character out of place.
Answer by CyberGolem · Mar 31, 2014 at 04:58 AM
SOLVED!
I found an unnecessary semi-colon in one of my lines of code [line 26 above]. So, Thank you very much getyour411! Your suggestions prompted me to look closer at my code.