- Home /
C# Children of GameObject has it's script disabled after SetActive(false) and SetActive(true)
Here's the situation:
A have a bunch of buttons and an Input Text Field as children to a Panel. I need to set them invisible at the start of the scene and visible after some conditions are met. For that i use this:
public void HidePanel()
{
Panel.gameObject.SetActive(false);
}
.....
.....
.....
.....
public void ShowPanel()
{
Panel.gameObject.SetActive(true);
}
After I set the Panel active, it sure does pop-up in the scene and the buttons attached to it too. However the buttons animation are dead and so is the InputTextField I have in there too. I can't even type in the Input Text Field. I use the regular buttons and Input Text Field that come with Unity.
I tried putting the panel in a different Canvas, same thing happened. I tried putting the panel in a differente Scene, same result.
Please help, I have been in this situation for the past 4 days and I can't be stuck in this situation for much more time, for I need to delivery this project soon.
My english is not the best, so I'm sorry if it is a confusing question. If you need more information, please ask.
Answer by Ermiq · Aug 27, 2018 at 07:07 PM
Try to add 'OnEnable()' method in the panel's script and call an initialize stuff there to re-enable/re-assign all it's components. When you have disabled a gameObject it may 'forget' all its stuff. So, if you have some code that initializes animations and other panel stuff in its start or awake, drag this code in a separate method and call this method in OnEnable():
void Start() {
Init();
}
void Init() {
// all your panel initialization code here
animations = GetComponent<Animator>();
text = GetComponent<Text>();
//
}
void OnEnable() {
Init();
}
Well that did work for other animation issue that I did not post here, but the real issue that some other user told me was that I forgot to add the Event System to the scene, hehe... big fail.
However your answer sure did help me in this other issue were animations were missing too. So: thank you very much @Ermiq :)
Your answer
Follow this Question
Related Questions
Creating a GameObject variable without instantiating it? 1 Answer
How to detect an object which be in FOV of certain camera ? 1 Answer
how to change a GameObject from another script 1 Answer
Access water4 scripts via my script? 1 Answer
how to change gameobject color in hierarchy for highlight purposes. 3 Answers