- Home /
GameObject.setActive(true) not working despite children and parents are active
As the title says, I cannot get a gameobject to become active. Its a UI panel object that is supposed to display after a fight and give a You win or You lose box to the player.
public class OutcomeScript : MonoBehaviour {
public GameObject outcomePanel;
void Start () {
outcomePanel.SetActive(false);
}
public void Outcome(int outcome) {
if(outcome == 0)
{
outcomePanel.SetActive(true);
outcomePanel.GetComponentInChildren<Text>().text = "You Win!";
outcomePanel.GetComponentInChildren<Button>().GetComponentInChildren<Text>().text = "Continue...";
}
else
{
//"You lose" stuffs
}
}
I have confirmed that the function to set the object to active is being called correctly and passing the correct integer. I have also confirmed that the parent of the object (the UI canvas) is active and all of its children are active as well.
I'm not sure where to go from here, any help is appreciated.
Note: I'm in Unity 5.5.3 if that has any effect
The only thing that comes to $$anonymous$$d for me is that your Integer, outcome is not being set as 0? so the outcomePanel and its child references cannot be called?
Is there another script that sets this integer to become 0? If so, is that doing its job when it should be?
I may be wrong I'm not too familiar with ints and how they work exactly.
put some Debug.Log()'s there to see where the code goes.. maybe some other part of the code disables it after that?
Can you enable it manually from hierarcy, while its running?
Yes, the checkbox is to the left of the object's name at the very top of the inspector.
Is the OutcomeScript attached to the outcomePanel GameObject? If so, the script will not run when it is inactive.
Answer by davidcox70 · Apr 05, 2018 at 10:41 PM
It should work so look at some more fault-finding. For example, if you comment out the line outcomePanel.SetActive(false) in Start(), does the panel show? This might help narrow down if something else is also setting the panel inactive. Also view the hierarchy in the editor when you run your game. This will show if any parent is being made inactive which affects it's children.
Your answer
![](https://koobas.hobune.stream/wayback/20220612155837im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
2020 Unity. How do I set children of a parent object to be active? 2 Answers
Detect UI Panel click 2 Answers
gameObject.SetActive (true); Not working 10 Answers
UI Animation Not Playing 1 Answer
Something in this Script keeps activating UI-Buttons 1 Answer