- Home /
How to store gameobjects with specific scripts in a list ?
Hello! So i`ve made a simple script to store gameobjects with script AIMOB in a array, the question is : How to store these gameobjects in a list ? Thanks for any help!
using UnityEngine; using System.Collections; using System.Collections.Generic;
public class AI : MonoBehaviour {
public AIMOB[] AImobs;
// Use this for initialization
void Start ()
{
AImobs = FindObjectsOfType(typeof(AIMOB)) as AIMOB[];
}
}
You can create a list out of an array of GameObjects by using the following line of code List<"GameObject"> listOfAImobs = new List(AImobs);
You also need to have using System.Collections.Generic;
at the beginning of the file, as you have done already.
Answer by Tarlius · Jun 03, 2013 at 11:17 AM
I believe you can just do
List<GameObject> aiMobsList = new List<GameObject>(AImobs);
but failing that you can add them with the List.AddRange method, or iterate over them and append one by one with the List.Add method.
I would question the need to do this though if you are using FindObjectsOfType to find instantiated objects instead of maintaining the list yourself, however. Do you intend to add/remove items at runtime also?
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
How do i convert this code snippet to a list? 1 Answer
A node in a childnode? 1 Answer
Distribute terrain in zones 3 Answers
Scripting and Coding Dictionary 0 Answers