Prefab Enemy2 to track all prefab Enemy1's Instantiates transforms: How?
So I've tried a number of ways of doing this, but none have worked so I need help. I have Prefabs, each with their own script, for the sake of this, Enemy1 with Script1 and Enemy2 with Script2. Then I have a GameObject, Spawner, with ScriptSpawner. The Spawner Instantiates Enemy1, about 100 of them, into a List, now what I want to do is pass that list into Enemy2, so that Enemy2 can go through all the Instantiates of Enemy1 and their transforms, so it can detect their location and their AI can respond accordingly.
How do I do this? Or is there a better way for Enemy2 to detect all the Instantiates of Enemy1 location?
Many thanks in advance.
Answer by TBruce · Apr 21, 2016 at 04:39 PM
You can add something like this to the Enemy2 script
private List<Enemy1> enemies;
void Start ()
{
enemies = new List<Enemy1>(FindObjectsOfType<Enemy1>());
if (enemies.Count > 0)
{
for (int i = 0; i < enemies.Count; i++)
{
Transform enemiyTransform = enemies[i].gameObject.transform;
// do what you want here
}
}
}
You sir, are a miracle worker!!! 3 Days and countless hours wasted, and it was as simple as that! Thank you!
How might I do this if I wanted to use an Array ins$$anonymous$$d?
Your answer
Follow this Question
Related Questions
Unable to set position of instantiated prefab 1 Answer
I need help destroying a clone on function. 1 Answer
How to teleport player to an Instantiated Prefab 1 Answer
Saving Instantiated objects,Saving Instantiated objects 0 Answers
how do i instantiate multiple object object with line change with for loop. 1 Answer