Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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
1
Question by calum1904 · Mar 14, 2017 at 05:30 PM · c#uigameobjectgameobjectssetactive

gameObject.SetActive (true); Not working

So I am making a game over UI and setting the game object that it's in at to inactive when I start the game but I can't get it to reactivate.

 public class gameManager : MonoBehaviour {
 
     public GameObject gameOverUI;
 
     void Start () {
 
         Debug.Log ("turn off ui");
         gameOverUI.SetActive (false);
 
     }
 
 public void gameOver(){
 
         Debug.Log("GameOver");
         gameOverUI.SetActive (true);
     }

This is a condensed version of my gameManager code, both of the Debugs are printing and when I have the gameObject on it disappears when I start the game so it must be linked correctly. I just can't get it to show again. Anyone know why?

Comment
Add comment · Show 2
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 ShadyProductions · Mar 15, 2017 at 02:03 PM 0
Share

Are you sure gameOver() method is being called? Or that its not another part of your script that is modifying this behaviour?

avatar image pr_0_0_ · May 28, 2021 at 08:11 AM 0
Share

I have a similar problem. In one scene my this code is working absolutely perfectly but in the next scene it is not working. The hypocrisy.

10 Replies

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

Answer by MeronSoda · Mar 15, 2017 at 03:35 PM

The "gameOver" function must be a callback from an active object. So... Is the object that calls this function activated?

For instance, if the "gameOver" callback is running from an Update function from an INACTIVE object, it won't be called.

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
5

Answer by body · Mar 16, 2017 at 08:29 PM

Hi! I had same problem, but i found why this is happens.

  1. You made GameObject in design time.

  2. Made it inactive.

  3. Start play

  4. GameObject is still inactive.

  5. You call gameOver()

  6. GameObject activates first time

  7. Unity calls Start() method (because script activate first time.). GameObject deactivated again.

So! You have two variants:

  1. Makes GameObject initially as inactive (in Design Time) and remove deactivation from Start() method.

  2. Makes GameObject initially as active (in Design Time) and keep deactivation in Start() method.

Good luck.

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 Hauthorn · Aug 13, 2019 at 10:17 PM 0
Share

#6 won't happen (and so #7 won't either).

An inactive object doesn't benefit from lifecycle events. You can call functions on inactive objects (doing so will not activate an object), but if you rely on lifecycle events to initialize anything used by your function, you'll find null reference issues.

avatar image
0

Answer by Matt1000 · Mar 16, 2017 at 04:25 PM

Your code seems to be perfectly ok. The only posible problem i can imagine of is a hirerchy problem. If you have a parent/grandparent GameObject disabled, this fact will have prority and the GameObjectt you want to activate wont. Perhaphs this can help you.

Otherwise check if your code depends on that other object. But i dont think it's the case scince you are using a game manager.

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 KrazyApple · Mar 15, 2017 at 02:21 PM

Hello , Did you check the call of your function gameOver ? Or maybe you setActive(false) the gameOverUI after the function or in others scripts ?

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 joshua-lyness · Mar 15, 2017 at 02:28 PM

I might be wrong, but surely if you have a script on an inactive gameObject, that script won't be running?

From the looks of your code, the GameOverUI is a variable, so if you had a manager object separate from the UI object then it would run fine, however you say in the question "the game object that it's in", so thought it was worth checking.

If it was the latter, make a new gameObject that is always active, and move the script onto that. It can be a "manager" object that handles activating UI and so forth. Just a heads up as well, if you want to access the gameObject that a script is attached to, "gameObject" can be used instead of making a variable to hold itself. :)

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
  • 1
  • 2
  • ›

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

20 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

Related Questions

Game Over Screen appears on launch.,Something with my script makes it so that everytime I run it a gameover happens 1 Answer

How to Use DontDestroyOnLoad on my C# Script? 1 Answer

3D arrangement of collection of gameobjects 0 Answers

If Check Colliding GameObject Variable 1 Answer

Script Two Button Functions At Once 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