- Home /
GetComponent for all enemies
I have a newbie question. I'm using GetComponent (it's in the player's main script) to change the color of nearby enemies, and it only works on the most recently created enemy. Is there an easy way to have all enemies pick that up?
SendMessage acts the same way.
Edit: Here is the answer
Just in case anybody else comes looking this way, what I needed was an array and a for loop in the script that's wanting the information
var enemyArray = GameObject.FindGameObjectsWithTag("Enemy");
for (var plState in enemyArray) {
plState.SendMessage ("PlayerState", 0);
}
or you could use a static variable for the script giving information
static var aState : int;
function Update () { aState = 2;
and this in the script that wants the information
plState = PlayerLogic.aState;
Although I don't know why you would use an array, it seems to be inefficient compared to a static variable (although I'm sure there are cases when a static variable won't work).
Your answer
Follow this Question
Related Questions
Accessing an Array in another function 1 Answer
how to make one script change a variable in another scipt 2 Answers
How to call a function from another script in C# from array? 1 Answer
once again, Sendmessage VS Getcomponent! 1 Answer
Obtaining an array of positions from an array of gameobjects 2 Answers