I am having some issues populating a List that im using primarily to hold data about some game objects which in this case are pegs that are to be placed into slots in a cube.
I have declared the List as such
static public List<GameObject> pegObjects;
it is then initialised in the start function of the cubeClass which it is contained within :
pegObjects = new List<GameObject>();
It’s function is then to have the pegs added to the list once they are initialised. I am having trouble with something else so i’m currently just testing elements.
To test the List functionality I am instantiating a single peg by instantiating it to a game object declared in the pegClass as such :
using UnityEngine;
using System.Collections.Generic;
public class test : MonoBehaviour {
public Transform sphere;
private List<GameObject> gos = new List<GameObject>();
void Start ()
{
for (int a = 0; a < 5; a++)
{
Transform go = (Transform) Instantiate(sphere, transform.position, Quaternion.identity);
gos.Add(go.gameObject);
}
Debug.Log(gos.Count);
}
}
Ballache, I actually do have ‘as GameObject’ on the end of my instantiation code but I was typing it above and left it out.
And Appels that is something similar to what I need, however the instantiation only occurs when the player presses a button which is why I need to add the items to the list after the user has pressed the button which instantiates the pegs.
using UnityEngine;
using System.Collections.Generic;
public class test : MonoBehaviour {
public Transform sphere;
private List<GameObject> gos = new List<GameObject>();
void Update ()
{
if (Input.GetMouseButtonDown(0)) {
Transform go = (Transform) Instantiate(sphere, transform.position, Quaternion.identity);
gos.Add(go.gameObject);
Debug.Log(gos.Count);
}
}
}
Essentially that it what I have at the moment. In a seperate GUI script I have 4 call to four buttons, as there are four different coloured pegs. On clicking the button it calls one of four corresponding functions called, using Red as the example colour, “InstantiatePegRed” which is contained in the peg class. Within this function is this :
This then successfully instantiates the peg in the correct position, attaches the script and takes the gravity off the object. However it then logs the pegObjects.Count still as 0. So the problem I believe is that it somehow i’m not adding the object to the list correctly.
as you can see in my example it does add them to the list.
you can put the list in another class and make the list public and static, i fail to see why it shouldn’t work.
Your list is static, so it’s shared between all the mono behaviors of the same type. Each time you instantiate a new object, it creates a new instance of the list and stores it to the shared variable, effectively emptying the list. What you need is simply not to recreate the list if it already exists:
if(pegObjects == null)
pegObjects = new List<GameObject>();
With one exception - you don’t need to check for null if you do it like so:
Public class Cheese : MonoBehaviour
{
public static List<GameObject> ObjectsList = new List<GameObject>();
// Use this for initialization
void Start()
{
Print("No. of objects in list = " + ObjectsList.Count);
}
}