- Home /
Call Methods from inactive GameObjects
Hello,
I am having a problem with calling methods from GameObjects that are currently inactive. I am using object pooling to activate and deactivate enemies instead of destroying them. However when I try to call a method from all of them (even inactive ones) to boost their movement speed for example, it only works on the active enemies. Is this a limit? I cannot call methods from inactive game objects? Or I am doing something wrong?
I am using an array of gameobjects and assigning all gameobjects with certain tag to it.
here is an example of the code I am using:
GameObject[] enemies = GameObject.FindGameObjectsWithTag("Enemy");
for(int i=0; i < enemies.Length; ++i)
{
enemies[i].GetComponent<EnemyScript>().IncreaseStats(3f);
}
Thank you a lot in advance :).
Belzy
Answer by TreyH · May 02, 2018 at 12:02 AM
You can call methods for components attached to inactive game objects, yes.
Your issue is that GameObject.FindGameObjectsWithTag("Enemy");
won't find inactive objects.
Thank you for such a fast answer, you are right I should have noticed that in unity documentation...
Is there another way to find inactive objects? I tried using GetChild but it would just happen the same thing.
Okay never$$anonymous$$d, I don't know what I did wrong before then but now I tried again with Getchild and it worked.... thank you @TreyH :)