Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 CodexMaster · Sep 04, 2015 at 01:12 PM · unity 5uiprefabprefabssetactive

SetActive true not working on UI object

Hey everyone,

So I have run into a problem. I have a simple action where when the player runs out of lives a panel comes up.

My problem is the panel won't become active again, the script is not on the panel itself, it is on the main camera. Stranger still, it is only on certain scenes, some it works fine, same script, the scene is almost identical just a few minor differences to integer variables. I have tried fiddling around with it, I had that it would simply set the gameobject to true in a function, after that wasn't working I made it a bool and when the bool is true the panels setactive is true and when the bool is false, false.

I ran into a similar problem before as the script was trying to set the prefab to active instead of the scene in the object, to solve that I just added a GameObject.Find which worked fine. That has not worked in this case. The script finds the object at the start of the scene then sets its active false, but then cannot turn it back on.

Edit: After doing some tests, everything seems to work with objects that are not connected to a prefab. But if the object is connected to a prefab the problem appears, this is with any object I tried not just the one.

Edit2: Tested instantiating the object first and making the clone the OutOfLives object. Problem was still the same. Object can be turned off but won't come back on.

Here is the relevant code:

 void Awake () {
         OutOfLives = GameObject.Find ("Out Of Lives");
 }
 
 void Update () 
     {
             if (OflOpen) {
                 OutOfLives.gameObject.SetActive (true);
             } else {
                 OutOfLives.gameObject.SetActive(false);
             }
 }
 
 public void Restart()
     {
         if (GlobalStats.Lives >= 1) {
             Application.LoadLevel (Application.loadedLevel);
             GlobalStats.Lives -= 1;
         } else if (GlobalStats.Lives <= 0) {
                 OflOpen = true;
         }

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 wibble82 · Sep 06, 2015 at 10:17 AM 0
Share

Hey there

Could I just ask you to try doing the 'gameobject.find' every frame in update. This is not a good thing to do typically, but it would verify conclusively that you are finding the correct object.

-Chris

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by DiegoSLTS · Sep 04, 2015 at 02:43 PM

Have you debugged your code? It looks kind of messy but it should work I think. Make sure that OflOpen changes when it should (check if the execution enters the if(GlobalStats.Lives

I'd do it different though, try this code, it avoids unnecessary calls to SetActive on every frame.

 void Awake () {
     OutOfLives = GameObject.Find ("Out Of Lives");
     OutOfLives.gameObject.SetActive(false);
 }
  
 public void Restart() {
     if (GlobalStats.Lives >= 1) {
         Application.LoadLevel (Application.loadedLevel);
         GlobalStats.Lives -= 1;
     } else if (GlobalStats.Lives <= 0) {
         OutOfLives.gameObject.SetActive (true);
     }
 }
Comment
Add comment · Show 8 · 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 CodexMaster · Sep 05, 2015 at 04:37 AM 0
Share

Hey,

Yep I have debugged it, both in the update and in the code you sent. The debug comes up in both cases but it still isn't working. As I said it works in certain scenes but does not in others. I suspect it may have something to do with the objects relationship to the prefab.

I just tested a few different objects, it seems it works with objects that are not connected to a prefab, but does not work with objects that are connected to a prefab.

avatar image getyour411 · Sep 05, 2015 at 05:44 AM 0
Share

When you instantiate the prefab, is it made a child of something?

avatar image CodexMaster · Sep 05, 2015 at 01:01 PM 0
Share

The object does not instantiate, it's in the scene. I dragged the prefab from the project folder into the hierarchy. It is a child of the Canvas

avatar image CodexMaster · Sep 06, 2015 at 05:28 AM 0
Share

Just tried instantiating the object them making the clone object the OutOfLives gameobject. Problem still the same, it can turn it off but then can't turn it back on.

avatar image Rissa_Ja · Nov 14, 2015 at 09:37 PM 0
Share

hi @DiegoSLTS i am having a similiar problem, my setactive(true) doesn't work. my code is below

gameObject.SetActive (false);

function start () {

if(CoinSystem.coinscollect >= 1){

  gameObject.SetActive(true);

}

}

avatar image DiegoSLTS Rissa_Ja · Nov 14, 2015 at 09:56 PM 0
Share

function Start() { ... }

Note the "S" on the Start method, it should be in Uppercase. Casing is important when coding.

avatar image Rissa_Ja DiegoSLTS · Nov 14, 2015 at 10:10 PM 0
Share

Thanks, i fixed that but its still not showing up. i have no idea what's wrong :(

Show more comments
avatar image
0

Answer by CodexMaster · Sep 07, 2015 at 11:48 PM

Found what the problem was, the GameObject variable had to be static.

Not sure why that is, but adding static fixed the problem.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Display text above prefabs in Unity 4.6 0 Answers

Canvas with GUI elements in prefab act strangely 0 Answers

Runtime Changes to UI Object From Prefab Unexpectedly Changing All Prefab Objects 1 Answer

Game Object is Instantiating on same position 2 Answers

I need help with prefabs 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