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.

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 :smiley:

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);

function convertToList(tmpGol:GameObject):List.{
var xz : int;
var tmpList = new List.();
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);
	}
}

Just to clear the caps mistake in the above code:

     	playerList.Add((GameObject) Instantiate (prefab, rndPos, Quaternion.identity));