- Home /
Fill an Array with GameObjects from another script
Hi Everyone, let me explain the situation I'm trying to solve:
A have a script that runs on start and selects 2 objects from a large array of GameObjects. We'll call those objects ObjectA and ObjectB. So at start up I end up with a Transform defined for ObjectA and one for ObjectB. I need those transforms to be read by another script attached to the same object and then loaded into a new array.
Example:
Script 1
BigArray[ 1 ]
BigArray[ 2 ]
BigArray[ 3 ]
BigArray[ 4 ]
BigArray[ 5 ]
BigArray[ 6 ]
From this array my first script will select 2 GameObjects which will now be called ObjectA and ObjectB. What I would like to do here is assign each of these GameObjects to a specific array position in another script attached to the main object. If possible I would like all the work to be done in script 1.
Script 2
ObjectArray[ ObjectA ]
ObjectArray[ ObjectB ]
Any help with this would be really appreciated.
I'm doing the script in C# but a Javascript example would be helpful as well.
Thanks.
Answer by homer_3 · Feb 21, 2012 at 01:29 AM
If I understand you correctly, Script1 and Script2 are applied to the same game object. Add getters to script 1 that will return ObjectA and ObjectB. In Script2 try
var foo : Script1 = gameObject.GetComponent("Script1");
foo.GetObjectA();
foo.GetObjectB();
Obviously, this is a javascript example.
Your answer
![](https://koobas.hobune.stream/wayback/20220613064257im_/https://answers.unity.com/themes/thub/images/avi.jpg)