- Home /
space shooter tutorial, ending the game
Hello all
This is my first time posting here, so sorry if my question is dumb, or obvious.
I am currently running through the space shooter tutorial on the unity website. It's a great tutorial and I have really enjoyed making the game, I am now on the penultimate part, titled "Ending The Game", as far as I can tell I my code is right and everything else is too. Although obviously something is wrong.
I am trying to set up an end to the loop and to make the game restart on the press of the 'R' button.
Here is my 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);
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);
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;
}
}
and here is my DestroyByContact script:
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);
}
}
Any help would be very much appreciated.
All the best
Harry
You need to add some information of what exactly is going wrong. =) What happens when you reload the level? Or is it not getting to that part?
Hi sorry about that, yeah it doesn't reach the reload or game over part. Once the ship get's hit by an asteroid, thats it nothing happens. There is no game over text and no 'click R for restart' text.
$$anonymous$$aybe you misspelled the tag "player"? Try changing it to "Player"..
Line 31, when you check if the collider tag is actually the player, if it is, an explosion will be instantiated and you'll be telling the gameController to end the game, with the GameOver method.. If the string "player" is not really "player", the game is gonna be endless.
Thanks woodoo, i'm afraid I don't fully understand what you mean. The player game object does have the tag "Player", should I change the 'p' in 'player' in DestroyBycontact to a capital?
If there is no GameOverText then GameOver is probably not called. Which means that the DestroyByContact object does not know that it has collided with the player. As woodoo says, double check your tag and make sure it is correct. There may be something wrong with the collision as well, are there colliders on both the hazard object and the player? Does at least one of them have a rigidbody attached?
Edit: and yes, that is what he means, change the p to be capital if the Tag on the player is capitalized.
Answer by Benoit Dufresne · Jan 17, 2014 at 03:44 PM
The object that has DestroyByContact needs to have a "player" tag. This is case sensitive.
In the collision trigger, you check "if (other.tag == "player")". This tag is usually "Player" with a capital P.
Also make sure your collider has "isTrigger" checked in the inspector. Otherwise the function called is OnCollisionEnter instead of OnTriggerEnter.
For reference here is where you set tags:
For me, what worked was changing the 'Child' of the player object to have tag 'Player'. Apparently just having the 'Parent' object to the player object tagged was not enough.
Your answer
Follow this Question
Related Questions
Space Shooter problems 2 Answers
Having trouble with reloading the scene after you die in the project Space Shooter 1 Answer
Glitchy Animation 3 Answers
Callback from a Loop 1 Answer