Gather all GameObjects with a certain name in a Scene and Apply them to a list or Array?
Any help would be great! thanks
Gather all GameObjects with a certain name in a Scene and Apply them to a list or Array?
Any help would be great! thanks
it is can be helpful: http://answers.unity3d.com/questions/24257/how-do-i-find-all-game-objects-with-the-same-name.html
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class GOList : MonoBehaviour
{
List<GameObject> goList = new List<GameObject>();
string nameToAdd = "Cube";
void Start()
{
foreach(GameObject go in GameObject.FindObjectsOfType(typeof(GameObject)))
{
if(go.name == nameToAdd)
goList.Add (go);
}
print (goList.Count);
}
}
you could use Linq if you’re using C#
using System.Linq;
GameObject[] objs = GameObject.FindObjectsOfType(typeof(GameObject)).Select(g => g as GameObject).Where(g => g.name == "desired name").ToArray();
or, if you wanted it to be put in a list, you would do the exact same thing except create a List and end your Linq Query with .ToList() rather than .ToArray()