- Home /
How does one go about creating a list of GameObjects?
How does one go about creating a list of GameObjects?
I have created a class called Hex it has the follow variables:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class Hex : MonoBehaviour {
[HideInInspector] public GameObject hex;
[HideInInspector] public int[] weightValue;
[HideInInspector] public GameObject[] adjacentHexs;
[HideInInspector] public GameObject[] connected;
static public List<Hex> hexList = new List<Hex>();
//Defualt constructor
public Hex(GameObject addHex)
{
hex = addHex;
}
//Advanced constructor
public Hex(GameObject addHex, int[] addWeightValue, GameObject[] addAdjacentHexs, GameObject[] addConnected)
{
hex = addHex;
weightValue = addWeightValue;
adjacentHexs = addAdjacentHexs;
connected = addConnected;
}
// Use this for initialization
void Start () {
foreach (GameObject hexEmpty in GameObject.FindGameObjectsWithTag("Hex"))
{
print("Adding to list");
hexList.Add (new Hex(hexEmpty));
}
}
}
I also have this script:
using UnityEngine;
using System.Collections;
public class HexMaster : MonoBehaviour {
void Start ()
{
foreach (Hex hexItem in Hex.hexList)
{
print(hexItem.hex.transform.position.ToString());
}
}
}
But when I run the script I get the following error:
"You are trying to create a MonoBehaviour using the 'new' keyword. This is not allowed. MonoBehaviours can only be added using AddComponent(). Alternatively, your script can inherit from ScriptableObject or no base class at all"
How would one go about creating a list of gameobjects without using MonoBehaviour?
The code works I just get the error/warning so I am guessing there is a "proper" way of doing this that I cant figure out?
Answer by VioKyma · Nov 10, 2013 at 02:24 AM
You cannot use a constructor for a MonoBehaviour or use the "new" keyword (same goes for GameObjects). You want to use Instantiate and add the returned game object to the list. Unity already has a constructor that runs on these things, so writing your own breaks Unity's functionality. You have one of two options here: 1. Remove the MonoBehavior, and lose the inspector functionality OR 2. Stop using the constructors, and instead use Start() and other methods to initialise your objects.
To create the gameObject list do something like the following:
static public List hexList;
void Start()
{
hexList = new List<Hex>();
}
To add items to the list do something like this:
GameObject hexItem = Instantiate(...instantiation details...);
hexList.Add(hexItem);
If you want a list of prefabs to be available to then instantiate later, you need to use the Inspector to assign these.
Let me know if you need more detail or have any questions.
To clarify the final statement a bit, Prefabs are only available to your script if they are passed in via the Inspector.
Thank you for your help. I am not sure how I would use Instantiate the GameObjects I want to add are already in the scene?
Im not sure I understand,
I have a list that holds a class called "Hex" the hex class has a variable that is a gameobject.
So I need to add a new Hex before I can add its gameobject?
O$$anonymous$$ I think I have this sorted now thanks Vio$$anonymous$$
I wrongly assumed that when you created a script without inheriting from $$anonymous$$onoBehaviour that you lost ALL access to Unity Stuff but you can still use a GameObject as variables.
Code looks like this:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class Hex {
public GameObject hex;
public int[] weightValue;
public GameObject[] adjacentHexs;
public GameObject[] connected;
static public List<Hex> hexList = new List<Hex>();
//Defualt constructor
public Hex(GameObject addHex)
{
hex = addHex;
}
}
and
using UnityEngine;
using System.Collections;
public class Hex$$anonymous$$aster : $$anonymous$$onoBehaviour {
void Start ()
{
//Add Hexs to list
foreach (GameObject hexEmpty in GameObject.FindGameObjectsWithTag("Hex"))
{
Hex.hexList.Add (new Hex(hexEmpty));
}
//print there locations
foreach (Hex hexItem in Hex.hexList)
{
print(hexItem.hex.transform.position.ToString());
}
}
}
Thank you again :D