- Home /
Create array of scripts
This example best illustrates what I would like to do (while also drawing attention to my ignorance!) :P
var allWindows = new WindowHealth[7];
function Start ()
{
allWindows = GameObject.FindGameObjectsWithTag("Window").GetComponent(WindowHealth);
}
Answer by Essential · May 22, 2012 at 01:44 AM
Okay, I'm unsure if there's a better approach but this seems to be working alright:
var allWindows = new GameObject[7];
var allWindowsHealth = new WindowHealth[7];
function Start ()
{
allWindows = GameObject.FindGameObjectsWithTag("Window");
for ( var i = 0; i < 7; i++)
{
allWindowsHealth[i] = allWindows[i].GetComponent(WindowHealth);
}
}
Nope, I just typed up a quick example for you that looks pretty much like that.
You could rewrite this so that it doesn't assume it'll find 7 hits exactly -- allWindows.Length
-- or make allWindows
a local variable ins$$anonymous$$d of a persistent one. Both of these are $$anonymous$$or quibbles if you're just starting out, though.
Your answer
Follow this Question
Related Questions
Accessing an Array in another function 1 Answer
how would I make an array of different scripts 1 Answer
adding all scripts in current gameobject to an array 1 Answer
In an array of scripts, how to access their variables? 2 Answers
Obtaining an array of positions from an array of gameobjects 2 Answers