How to activate and deactivate child of an instantiated parent object,
I have created a game manager that fills a list of enemies. Anytime the player kills one of the enemies, it is removed from this list. When the list reaches 0, the list is refilled with more enemies. The enemies I am using is a prefab. This prefab has a child object that acts as a spawner for where the enemy will shoot from. The issue I am getting is that when the enemy is destroyed, the child projectile spawner remains and continues to fire while the enemy itself is destroyed. When I try to simply delete the child, it does not re-instantiate when a new enemy is created. How can I activate and deactivate this child? Would that even do anything since the spawner would still be in the same location of the last destroyed enemy? Should I change how I am finding where the enemy should fire from so I do not need a child? (Also I am programming in C#)
Thanks!,
doesn't make sense. when you really destroy a gameobject all children will be destroyed along with it.
Answer by srivello · Feb 16, 2017 at 12:01 PM
Two thoughts.
In response to your question title...
//Toggle activate
bool isActive = mychild.gameObject.activeSelf;
mychild.gameObject.SetActive(!isActive);
In response to your question body...
//In the script for the Enemy 'child' within the prefab...
gameManager.EnemyList.Remove(transform.parent);
Destroy(transform.parent.gameObject);