- Home /
Gather all GameObjects with a certain name in a Scene and Apply them to a list?
Gather all GameObjects with a certain name in a Scene and Apply them to a list or Array?
Any help would be great! thanks
Answer by clunk47 · Nov 07, 2013 at 08:55 PM
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);
}
}
Thanks $$anonymous$$an! I can always count on you for good answers. Works perfectly.
Try something like this:
using UnityEngine;
using System.Collections;
public class ArrayExample : $$anonymous$$onoBehaviour
{
public GameObject[] goArray;
public string nameToArray = "Cube";
void Start()
{
GameObject[] temp = (GameObject[])GameObject.FindObjectsOfType(typeof(GameObject));
int count = 0;
foreach(GameObject go in temp)
{
if(go.name == nameToArray)
count++;
}
goArray = new GameObject[count];
for(int i = 0; i < goArray.Length; i++)
{
if(temp[i].name == nameToArray)
goArray[i] = temp[i];
goArray[i].name = nameToArray + " - " + i.ToString();
}
temp = null;
foreach(GameObject go in goArray)
{
print (go.name);
}
}
}
Never $$anonymous$$d! Got it to work. lol here is my code converted into JS.
Basically what this does is (in execution order) -
-Creates buildings/objects on start - -finds all buildings in scene - -finds specific name type buildings - -Puts newly created buildings in new Array - -Numbers the buildings from 1 to 10 for later use - -nulls/clears out the AllObjects array-
import System.Collections.Generic;
var AllObjects: GameObject[];
var $$anonymous$$ediumCommericalBuildings: GameObject[];
var $$anonymous$$ediumCommerical_New : Transform;
var Name = "$$anonymous$$ediumCommerical_New";
function Start () {
//Object creation
for (var A : int = 0;A < 10; A++) {
$$anonymous$$ediumCommerical_New = Instantiate ($$anonymous$$ediumCommerical_New, Vector3(A * 4.0, 0, 0),transform.localRotation);
$$anonymous$$ediumCommerical_New.name = Name;
}
//Find All Objects
AllObjects = FindObjectsOfType(GameObject);
//Creates New Array According to name specifc buildings in AllObjects and creates the correct amount of elements for that Array
var count: int = 0;
var GO: GameObject;
for(GO in AllObjects){
if(GO.name == Name){
count ++;
}
}
$$anonymous$$ediumCommericalBuildings = new GameObject[count];
//Pushes the new Buildings in New Array
for(var i = 0; i < $$anonymous$$ediumCommericalBuildings.Length; i++){
if(AllObjects[i].name == Name){
$$anonymous$$ediumCommericalBuildings[i] = AllObjects[i];
$$anonymous$$ediumCommericalBuildings[i].name = Name + "_" + i.ToString();
}
}
AllObjects = null;
}
function Update() {
if (Input.Get$$anonymous$$ouseButtonDown(0)) {
var hit: RaycastHit;
var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, hit)) {
if (hit.collider.gameObject.name == "$$anonymous$$ediumCommerical_New_0"
|| hit.collider.gameObject.name == "$$anonymous$$ediumCommerical_New_1"
|| hit.collider.gameObject.name == "$$anonymous$$ediumCommerical_New_2"
|| hit.collider.gameObject.name == "$$anonymous$$ediumCommerical_New_3"
|| hit.collider.gameObject.name == "$$anonymous$$ediumCommerical_New_4"
|| hit.collider.gameObject.name == "$$anonymous$$ediumCommerical_New_5"
|| hit.collider.gameObject.name == "$$anonymous$$ediumCommerical_New_6"
|| hit.collider.gameObject.name == "$$anonymous$$ediumCommerical_New_7"
|| hit.collider.gameObject.name == "$$anonymous$$ediumCommerical_New_8"
|| hit.collider.gameObject.name == "$$anonymous$$ediumCommerical_New_9"){
Destroy(hit.collider.gameObject);
}
}
}
}
Thanks a lot for your help!
No problemo dude. Thanks for posting your JS version for others to see.
Answer by shadows_s · Nov 07, 2013 at 07:21 PM
it is can be helpful: http://answers.unity3d.com/questions/24257/how-do-i-find-all-game-objects-with-the-same-name.html
Answer by zombience · Nov 07, 2013 at 07:14 PM
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()
this is something you should not do every frame. Linq tends to be slow and will bog down performance if overused.
Your answer
Follow this Question
Related Questions
C# Array problems 2 Answers
Change List name as the "Name" Property in the list 1 Answer
How to create an array out of many gameObjects? 1 Answer
Multiple Fire Points 2 Answers
How you find an Object 1 Answer