I have this C# script on a trigger that gets the component of my main script;
Modus_Doors door1 = door1Triggered.GetComponent<Modus_Doors>();
But then I changed it to add multiple objects in an array. I was told I need a for loop to do something like that, so here is what I have;
for (int i = 0; i<doorsTriggered.Length; i++)
{
doors = doorsTriggered*.GetComponent<Modus_Doors>();*
}
But it only gets the component for one of the objects. What am I doing wrong and what do I need to change in order to do this correctly?
Part 1 Multiple scripts, one object
GetComponent returns the first found. GetComponents (with the s) returns a list of all the found ones.
Thus, you start with GetComponents, then loop.
Modus_Doors[] doors;
doors = gameObject.GetComponents<Modus_Doors>();
foreach (Modus_Doors door in doors) {
//Do something like door.blah = blah;
}
Part 2 Multiple doors, separate objects
At some point, create an array of your door objects:
Modus_Doors[] doors = new Modus_Doors[0];
As you create objects, grow the array.
Array.Resize<Modus_Doors>(ref doors, doors.Length + 1);
… And assign the new object to it:
doors[doors.Length - 1] = theNewObject.GetComponent<Modus_Doors>();
When you need to loop them, loop them:
for(int i = 0 ; i < doors.Length ; doors++){
doors*.blah=blah;*
}

i’ve tried reading through this, couldn’t find anything that fixes this. Hope i don’t waste anyones time. This could even be something not directly related.Thanks for the replies in advance.
i already have a question out there just trying to look for
similar situations.