Hi,
i have this strange Problem:
i have a ui prefab that represents an entry for a list. It has multiple child objects (buttons) that i want to set active depending on some variables.
When i gameObject.SetActive(…) the buttons before i SetActive(true) the parent (which at this point is already an instance of the prefab btw.), they are not being activated (if set to true), only when i reverse the order and activate the parent first.
Is this normal?
P.S. i can even step in with the debugger to see the call to SetActive(true) despite them being inactive afterwards…
I find that a strange analogy as i can enable and disable gameobjects by hand in prefabs and they stay this way, plus, i can’t believe that this behaviour is normal.
What i don’t mean, and maybe you just misunderstood it this way is that children are disabled with a disabled parent, that’s normal.
What i mean is that if i enable a child, then enable the parent, the child is still disabled. But if i enable the parent and then the children, everything is normal; the order seems to matter, which i can’t see why it should.
Thats odd.
However I used SetActive alot, but never needed to use it on tree based hierarchy with both parent and children having SetActive in action. So I am unable further to assist here.
I think it’s the same as when you press the On-button on a computer(the child), without the power chord (the parent) being in a socket, then the computer will still be off once you plug it into the socket. Only if you press the On-button after you plug the power chord into the socket, the computer will turn on.
Yes, the local state is what i mean, it’s set through SetActive() too.
“This state will then be used once all parents are active.” ← and this is the part that’s not working.
They are not becoming active once i activate the parent, as i wrote earlier, which may be what your power/socket analogy was meant to say though.
Why do you deactivate the children when only deactivating the parent will do the job?
If the parent is inactive, the children will be active in the hierarchy window but inactive in the scene. Then you just have to set the parent active to get everyone active in your scene.
That’s mostly what i want, yes. with the difference that i need to selectively enable and disable them (that’s changing between parent SetActive calls) before having possible OnEnable callbacks on them use variables that i initialize while the parent is still disabled (in a script on that parent).
I could probably implement a custom interface and GetComponents() all scripts with that interface and use a custom initializer function instead of OnEnable, but that’s more like a workaround and the documentation states that it should work whereas it does not, for me atleast.
Is it my installation only maybe?
I know as that is not what i want (if you’re talking about doing that from a unity callback). I want the children, the moment they become active (which is when their parent becomes active) to use those variables from that callback.
I have a reference to a parent. It’s deactivated. I set some variables on it. Then i selectively activate the children. Then i activate the parent. The children should become (actually) active with the parent. They don’t.
Please file a bug report with a minimum project which shows that it’s not working because it’s a bug.
ps.: it’s working for me in 2018.3b8.
I have three inactive gameobjects (I have unchecked the checkboxes on all three), one parent (p), two children (c1 and c2) and then in a separate script I call
public class Test : MonoBehaviour
{
public GameObject p;
public GameObject c1;
public GameObject c2;
// Start is called before the first frame update
void Start()
{
c1.SetActive(true);
c2.SetActive(true);
p.SetActive(true);
}
}
And when the application runs, all three are active.
Since it was working for me I don’t have a bug. I don’t know if @Flavelius has submitted a bug or not since he didn’t answer in this thread.
Since it was working for me in the same version you probably have some other issues. Is it an empty scene with a set up I described? Or do you have more things? Do you have errors by any chance? Or even warnings?
I don’t know what went wrong, but when i reopened the project some days later, it just worked normally, so i just ignored it; maybe the editor just needed a restart.
I am also having the Same Problem kind of.
I have a CANVAS, and there are different Panels like setting panel, level_selection panel game_Play panel.
So in my Game_Play Panel there is a Dialogue Panel which contains button and a Text to show text.
So when I Disable that dialogue panel by SetActive(false) it executes the code but really don’t disable it.
And none of any child component of game_play panel could disable ??? why…
If I disable Game_Play panel it get disable but not any of it’s child ???