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?
– MCHammond