- Home /
Send a message to multiple scripts within an Array - Javascript
I did a little bit of google searching and there are not many threads on this, and the API description doesn't clear it up for me.
I've got this script so far, but it's giving me errors.
#pragma strict
var populateArray: boolean = false;
var objects: GameObject[];
function Update ()
{
if (populateArray)
{
Populate ();
}
else
{
objects = [];
}
}
function Populate ()
{
objects = GameObject.FindGameObjectsWithTag("farmer");
for(var Cube in objects)
Cube.SendMessage("Test");
}
Basically I have several test cubes in my scene, all tagged "farmer". Some have a script named "script01" and some have a script named "script02" But both those scripts have a function called "Test"
I'm trying to create an array, fill it with every object in the scene, then execute the function "Test" to every object within the array, using send.Message.
However, I'm clearly missing a step here, can anyone offer some clarity into what I'm doing wrong?
And I wouldn't recommend using calling Populate() function in Update function
there are no errors, it just doesn't call the function. It will populate the array when I check the populateArray box to true, however.
I will try what you suggest Jamora, thanks.
Thanks Jamora, you nailed the problem! Convert comment to answer!
Answer by Jamora · Jan 25, 2014 at 04:49 PM
Showing the error would help in figuring out what exactly is wrong...
But as a blind stab in the dark, I'd suggest typing your foreach variable: for(var Cube : GameObject in objects)
Your answer
![](https://koobas.hobune.stream/wayback/20220613131416im_/https://answers.unity.com/themes/thub/images/avi.jpg)