Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by barrycook · Jan 08, 2014 at 11:24 PM · loopspace shooterrestartgame overended

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

Comment
Add comment · Show 7
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image GameVortex · Jan 08, 2014 at 11:43 PM 0
Share

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?

avatar image barrycook · Jan 09, 2014 at 01:40 PM 0
Share

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.

avatar image woodoo · Jan 09, 2014 at 01:44 PM 1
Share

$$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.

avatar image barrycook · Jan 09, 2014 at 01:53 PM 0
Share

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?

avatar image GameVortex · Jan 09, 2014 at 01:56 PM 0
Share

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.

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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: alt text


tag.png (51.9 kB)
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Cougmen · Jun 20, 2015 at 06:29 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

21 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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

*** Trigger Collider2D event only once *** 3 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges