- Home /
How to put gameObjects to the list?
I have gameObjects and I need to put them to the list, something like that:
`List<GameObject> unityGameObjects = new List<GameObject>();`
Is it possible? If so, how to instantiate them though in another script?
Any answer will appreciated.
You can add GameObjects to a list in this way. If another script is responsible for instantiating them then they will need to do something like have a reference to this listing script and be able to call a method that informs the listing script to make a change.
http://wiki.unity3d.com/index.php?title=Which_$$anonymous$$ind_Of_Array_Or_Collection_Should_I_Use?
$$anonymous$$y list notes :
// to use List
import System.Collections.Generic;
// declare
var inRangeList : List.< GameObject > = new List.< GameObject >();
// Add object to list :
inRangeList.Add( theItem );
// length
inRangeList.Count;
// from position
inRangeList[ v ];
// empty list
inRangeList.Clear();
// send to builtin array
inRangeList.ToArray();
Answer by Timurov · Nov 22, 2012 at 11:56 AM
I'm answering my question: How to instantiate the list of game object in another script? Very easy, it is important to declare the list as public and use the name of the script where the list of game object is located:
xmlreader.unityGameObjects.transform.localPosition = new Vector3(6,8,9);
Answer by dukearena · Nov 22, 2012 at 09:38 AM
Hi! The list you posted is correct, you can try this code for your question:
// On Top ---
include System.Collections.Generics;
// Inside class --
public GameObject prefab;
List<GameObject> goList;
void Start(){
goList = new List<GameObject>();
}
void Update() {
if(Input.GetKeyDown(KeyCode.I) {
// This random position is for fun :D
Vector3 rndPos = new Vector3(Random.Range(-20, 20), Random.Range(-20, 20), Random.Range(-20, 20));
// Create a new GameObject from prefab and move to random position
goList.Add((GameObject)Instanciate(prefab, rndPos, Quaternion.Identity);
}
if(Input.GetKeyDown(KeyCode.L)){
Debug.Log(goList.Count);
foreach(GameObject go in goList){
Debug.Log(go.name);
}
}
}
NOTE: I wrote "goList = new List();" in start because I like the good sintax programming, you should write "List goList = new List();" in declarations
Now, the complete example, modify Update and Start like following:
OtherClass other;
void Start(){
other = GetComponent<OtherClass>();
}
void Update() {
goList.Add(other.myGameObject);
}
Now, create the OtherClass file:
public GameObject myGameObject;
public Start(){
myGameObject = (GameObject)Instanciate(whateverPrefab);
}
I hope your question is satisfied.. I'm sorry if I made mistakes, I wrote the code at the moment :D
Hi! your good sintax program$$anonymous$$g helped me to another list issue about random audio into a List . Thanks @dukearena !
Answer by jaxx0rr · Sep 18, 2013 at 06:47 AM
function convertToList(tmpGol:GameObject[]):List.<GameObject>{
var xz : int;
var tmpList = new List.<GameObject>();
for (xz=0;xz<tmpGol.Length;xz++){
tmpList.Add(tmpGol[xz]);
}
return tmpList;
}
function searchAnotherRandomEnemy(){
var unitPointList1 = new List.<GameObject>();
var unitPointList2 = new List.<GameObject>();
var unitPointList = new List.<GameObject>();
if (searchTimer > 0) {
searchTimer--;
return;
}
searchTimer = 10;
if (GNDturret) {
unitPointList1 = convertToList(GameObject.FindGameObjectsWithTag("Enemy"));
unitPointList.AddRange(unitPointList1);
}
if (AAturret) {
unitPointList2 = convertToList(GameObject.FindGameObjectsWithTag("EnemyAir"));
unitPointList.AddRange(unitPointList2);
}
print (unitPointList.Count);
if (unitPointList.Count > 0){
var randomO = Random.Range(0, unitPointList.Count);
currentEnemy = unitPointList[randomO];
EnemyScript = currentEnemy.GetComponent(AIEnemy);
}
}
Answer by davidnibi · Oct 11, 2019 at 11:19 PM
Just to clear the caps mistake in the above code:
playerList.Add((GameObject) Instantiate (prefab, rndPos, Quaternion.identity));
Your answer
Follow this Question
Related Questions
What is the best way to instatiate an array of connected GameObjects? 0 Answers
Can´t instantiate objects in list correctly 1 Answer
Can't Activate a GameObject from Array 1 Answer
How would I find the name of all game objects in a c# List 1 Answer
Store Game Object Into List For Later Reinstantiation? 0 Answers