- Home /
How to Fill Transform array using script ?
Hello! I have a prefab in Resources folder. This prefab has script which has the following line in script:
var AllItems: Transform[];
In Hierarchy I have objects, like Item1, Item2, Item3... Now i have to manually drag the objects in to this prefabs script. But I need some kind of script to do this automaticaly for example at the Start() when the prefab is instantiated The script searches for i dont know - a word "Item" or what to add all objects with this name to the Transform[] array. How to script this ?
Thanks!
Answer by GameVortex · Jan 22, 2014 at 08:53 AM
I would recommend using the function **FindGameObjectsWithTag** to find all the GameObjects **Tagged** with a specific tag and then fill your Transform array with the transforms of the found GameObjects.
Basicaly I did like this:
var checkPointArray : Transform[];
function Start () {
checkPointArray = GameObject.FindGameObjectsWithTag("Checkpoints");
Got Error: BCE0022: Cannot convert 'UnityEngine.GameObject[]' to 'UnityEngine.Transform[]'.
Two questions, How to get in this case the Transform from GameObject? And second, How to correctly fill the array with found GameObject Transforms ?
Thanks!
To get the transform from the GameObject just access it using: gameObject.transform.
The rest is very basic array functionality. The function returns an array of GameObject. So first store the return in a temporary GameObject array.
Initialize your transform array with the same length as the GameOBject array.
Iterate through the GameObject array and place the transform of each GameObject into the same position of the transform array.
Function returns an array of GameObject, but these GameObjects are like not in right order(they are not in ascending order), can I do some kind of Sort ? I read that there is function Array.Sort(). But this function doesnt work for me, becouse I dont have defined Array by ...= new Array() , but ins$$anonymous$$d as var checkPointArray : GameObject[];
I have got the following code. I now find all GameObjects with tag "Checkpoints" and convert them to Array() in order to Sort them. Then I convert them back to Built in array. Now question , how to correctly copy or convert to other array which holds not GameObjects, but theyr transforms ? I kind a think about for loop at the bottom of script.
function Start () {
var gos : GameObject[];
var names = new Array();
var dog = new Array();
gos = GameObject.FindGameObjectsWithTag("Checkpoints");
for (var go : GameObject in gos) {
names.Push(go.name);
}
names.Sort();
for (var name : String in names) {
for (var go : GameObject in gos) {
if(go.name==name){
dog.Push(go);
}
}
}
return dog.ToBuiltin(GameObject);
// Change GameObjects array to Gameobject.transform array for (var Checkpoints in gos) {
}
Your answer
Follow this Question
Related Questions
Use editor icons in game? 2 Answers
How to respawn items along with player 1 Answer
Picking up flashlight? 0 Answers
Item collection in specific sequence. 2 Answers