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 TheBigBang · Oct 16, 2014 at 10:18 PM · guigameobject

Gameover function calling before game ends help

Hey Unity Community i need some help wondering why my script is calling before the player dies. I have a gameover script that calls after the player loses all their lives and for some reason its called before the player dies. Here is the script for reference

Game Manager script

 public class GameController : MonoBehaviour    
 public Gameover GameOverScript;

 public int playerLives = 2;
 public GameObject player;
 public Texture shipTexture;

 void Update()
     {
         if (player == null && playerLives >= 1) {
             playerLives--;
             player = ((Transform)Instantiate (playerShip, new Vector3 (0, 0, 0), playerShip.transform.rotation)).gameObject;
         }
     }
 
         void OnGUI()
     {
         if (playerLives != 0) 
         {
             for (int i=1; i<=playerLives; i++)
             {
                 GUI.DrawTexture (new Rect (i * 36, 0, 750, 48), shipTexture, ScaleMode.ScaleToFit, true);
             }
         }
 
         if (playerLives == 0 && player == null) 
         {
             GameOver();
         }
 
     }
 
 
         void GameOver()
     {
             GameOverScript = GameObject.FindGameObjectWithTag ("GameOver").GetComponent<Gameover>();
     }

Game Over script

 void OnGUI()
     {
         const int buttonWidth = 120;
         const int buttonHeight = 60;
         
         if (
             GUI.Button(
             // Center in X, 1/3 of the height in Y
             new Rect(
             Screen.width / 2 - (buttonWidth / 2),
             (1 * Screen.height / 3) - (buttonHeight / 2),
             buttonWidth,
             buttonHeight
             ),
             "Retry!"
             )
             )
         {
             // Reload the level
             Application.LoadLevel("Stage1");
         }
         
         if (
             GUI.Button(
             // Center in X, 2/3 of the height in Y
             new Rect(
             Screen.width / 2 - (buttonWidth / 2),
             (2 * Screen.height / 3) - (buttonHeight / 2),
             buttonWidth,
             buttonHeight
             ),
             "Back to menu"
             )
             )
         {
             // Reload the level
             Application.LoadLevel("Menu");
         }
     }

I have the Gameover script attached to an empty game object is thats why its called earlier? Here is a pic of how it looks like. If you can help with this thank you!

Comment
Add comment · Show 1
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 DocMcShot · Oct 16, 2014 at 10:41 PM 0
Share

Have you tried to disable the Gameover script in the Awake() function on the Gameover script, then try to enable it in your Gameover function of your Game manager script? Something like:

 void GameOver()
 {
       GameOverScript =    GameObject.FindGameObjectWithTag("GameOver").GetComponent<Gameover>();
      GameOverScript.SetEnabled(True);
 }

I'm not 100% sure on this, but I don't know if you need the GameObject.FindGameObjectWithTag... line because you declared it public and will be setting it in the editor. I could be wrong though.

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by kamgru123 · Oct 16, 2014 at 10:37 PM

You have no mechanism to trigger the gameover. Your OnGUI method is being called right from the start. Best way would be to add a boolean to your gameover script that would check whether it should perform the OnGUI. Something like this:

 class GameOverScript : MonoBehaviour
 {
     public bool GameOver { get; set; }
     
     void OnGUI()
     {
         if (!GameOver)
             return;
         
         //rest of your code...
     }
 }

Then in your Game manager script in the GameOver() method you set the bool to true:

 void GameOver()
     {
             GameOverScript = GameObject.FindGameObjectWithTag ("GameOver").GetComponent<Gameover>();
             GameOverScript.GameOver = true;
     }
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 TheBigBang · Oct 17, 2014 at 01:29 AM 0
Share

Aha! Thank you it worked like a charm

avatar image
0

Answer by Michael-Thomas · Oct 16, 2014 at 10:34 PM

If I understand correctly that you've attached the GameOver script to an empty object and it's showing up immediately on game start then that makes perfect sense. The OnGUI method is always called while that object exists and it will always exist.

One solution would be to remove the GameOver script from the empty object and replace the GameOver() method in your GameController with:

 private void GameOver () {
     AddComponent<GameOver>();
 }

This will attach the GameOver component to the same object your GameController is attached to, but only after the correct conditions are met.

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Floating Enemy Health Bars? 1 Answer

Unity3D Game Time 1 Answer

Is it possible to link character skill lists to a GUI, and if so, how? 3 Answers

How To Make GUI Buttons Load/Quit 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