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 $$anonymous$$ · Aug 27, 2014 at 06:41 AM · guiplayercomponentsondestroy

Game Over GUI

I want to show a game over GUI without getting rid of anything else going on in the scene upon player death. But the player is destroyed upon their death, so when I add the GameOver component, it just gets destroyed right away (and gives me some errors). How can I get around this?

Here is what I have, although it does not work:

 //Death is a component of the player.
 //I have other stuff going on in death too,
 //so getting rid of it is something I want to avoid
 
 using UnityEngine;
 using System.Collections;
 
 public class Death : MonoBehaviour
 {
     private bool applicationClosing;
 
     void OnApplicationQuit()
     {
         applicationClosing = true;
     }
 
     void OnDestroy()
     {
         if (applicationClosing)
         {
             return;
         }
         //GameOver is my Game Over GUI Script
         gameObject.AddComponent<GameOver>();
     }
 }
Comment
Add comment
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

2 Replies

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

Answer by zharik86 · Aug 27, 2014 at 06:49 AM

The simplest method, it to add new object on a scene. I hope that you destroy only one GameObject, instead of all scene:

  void OnDestroy() {
   if (applicationClosing) {
    return;
   }
   //GameOver is my Game Over GUI Script
   GameObject gmo = new GameObject(); //create new object
   gmo.name = "myGUIscript"; //change name
   gmo.AddComponent<GameOver>(); //add your component at object
  }

I hope that it will help you.

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 HarshadK · Aug 27, 2014 at 06:53 AM 0
Share
  • for a complete code based solution.

avatar image
1

Answer by HarshadK · Aug 27, 2014 at 06:50 AM

You are adding your GameOver component to your player object which is getting destroyed so once your player object is destroyed that component will not be available since it is destroyed with your player object. And you might be trying to access the GameOver component on your player object from other scripts.

So what you can do is create an empty game object and add your GameOver component to that object and then enable that game object from your Death script. So even if your player is destroyed your GameOver component is available to access for other scripts from the empty game object to which it is assigned to.

This can be done something like this:

 public GameObject gameOverGameObject;
 
     void OnDestroy()
         {
             if (applicationClosing)
             {
                 return;
             }
             //GameOver is my Game Over GUI Script
             gameOverGameObject.SetActive(true);
         }

Here GameOverGameObject is the name of your empty game object which has the GameOver component attached to it already. Assing that Game Object to the gameOverGameObject variable from the script and also at the start set this game object to be disabled.

Comment
Add comment · Show 5 · 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 zharik86 · Aug 27, 2014 at 07:04 AM 1
Share

@Harshad$$anonymous$$ I am not sure, but GameObject.Find will not see deactivated objects(null reference exception)

avatar image HarshadK · Aug 27, 2014 at 07:09 AM 0
Share

Oh yeah! Didn't think of that. Thanks for noticing. Updated the answer.

avatar image $$anonymous$$ · Aug 28, 2014 at 01:28 AM 0
Share

I am a bit confused by this answer. If I already have a gameOverGameObject with a script attached to it, wont that script already run by default (before .setActive() is called)? Is it possible for me to create this gameObject and put my GameOver script inside of it inside of unity, and then just call gameOverGameObject.active(true); inside my code? Basically, to have it already exist but turned off, then to just flip a switch? (this was my original intent)

also, thanks for your answer :)

avatar image HarshadK · Aug 28, 2014 at 01:33 PM 0
Share

You need to set the game object to be disabled from the editor itself so that when you enable it, the attached script will be executed.

avatar image $$anonymous$$ · Aug 28, 2014 at 04:48 PM 0
Share

By that you mean uncheck it? I was unaware that this was a feature :)

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

1 Person is following this question.

avatar image

Related Questions

GUI not showing up on player destroy 1 Answer

GUI Pop-Up On Cube Collision 1 Answer

Restart with GUIText 2 Answers

Player Screen Resolution 2 Answers

Organizing Player Prefs? 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