- Home /
How to disable a script on a bunch of instantiated objects with tag ?
Ok I have enemyManger which instantiate a prefab that has a movement script attached to it . I created a tag so i can collect them later . now I do want to have a freeze bomb which disable their movement for a while but it seems not to work
IEnumerator Enemyfreezer()
{
yield return new WaitForSeconds(1f);
GameObject[] gameObjectArray = GameObject.FindGameObjectsWithTag("Zombie");
foreach (GameObject go in gameObjectArray)
{
go.GetComponent<EnemyMovement>().enabled = false;
}
yield return new WaitForSeconds(3f);
GameObject[] gameObjectArray1 = GameObject.FindGameObjectsWithTag("Zombie");
foreach (GameObject go in gameObjectArray1)
{
go.GetComponent<EnemyMovement>().enabled = true;
}
}
But when I replace the EnemyMovement with other component rather than a scripts it works just fine . like Animator component :
IEnumerator Enemyfreezer()
{
yield return new WaitForSeconds(1f);
GameObject[] gameObjectArray = GameObject.FindGameObjectsWithTag("Zombie");
foreach (GameObject go in gameObjectArray)
{
go.GetComponent<Animator>().enabled = false;
}
yield return new WaitForSeconds(3f);
GameObject[] gameObjectArray1 = GameObject.FindGameObjectsWithTag("Zombie");
foreach (GameObject go in gameObjectArray1)
{
go.GetComponent<Animator>().enabled = true;
}
}
Help Please :D
enabled only controls the Unity methods (Update, FixedUpdate, On$$anonymous$$ouseXXXX,...). If you are calling the movement from a controller that calls the move method, then this is still going. Is that so?
Answer by MiladZarrin1 · Oct 03, 2017 at 12:26 PM
Setting enabled to false, only make Update functions to stop. If you're using coroutines to move your enemy, or as @fafase told in comments, another scripts make the enemy move, then it won't work. I suggest you add Stop() and Resume() methods to your enemy class. When you call Stop() method, not only it will set enabled to false and stop all coroutines, it will also raise a flag (_movability = false) so the enemy refuses to move afterwards. Even if other components told it so. I think it will solve your problem.
Your answer
Follow this Question
Related Questions
how do i use component gathered from ontriggerEnter, outside of that function? 2 Answers
Light problem 1 Answer
Creating a GameObject variable without instantiating it? 1 Answer
Powerup issue - Speed 1 Answer
the player is not shooting 1 Answer