- Home /
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;
}
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
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);
}
}
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.
When you instantiate the prefab, is it made a child of something?
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
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.
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);
}
}
function Start() { ... }
Note the "S" on the Start method, it should be in Uppercase. Casing is important when coding.
Thanks, i fixed that but its still not showing up. i have no idea what's wrong :(
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.