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 CyberGolem · Mar 30, 2014 at 11:00 PM · loopspace shooterrestartgame over

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);
     }
 }
Comment
Add comment · Show 3
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 getyour411 · Mar 30, 2014 at 11:03 PM 0
Share

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?

avatar image CyberGolem · Mar 31, 2014 at 01:07 AM 0
Share

@Getyour411 :

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.

avatar image getyour411 · Mar 31, 2014 at 01:13 AM 0
Share

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?

2 Replies

· Add your reply
  • Sort: 
avatar image
0

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.

Comment
Add comment · 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
0

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.

Comment
Add comment · 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

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 tutorial, ending the game 1 Answer

Having trouble with reloading the scene after you die in the project Space Shooter 1 Answer

Screen doesnt refresh until after loop 3 Answers

Animation made in blender does not loop 1 Answer

Guarantee loop order of child objects. 1 Answer


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