- Home /
Can't assign an instantiated class to an array?
I use a for loop to instantiate a transform for each element in a Grid array; then I assign each element in the Grid array like such:
public Grid[] board;
for(int i = 0; i < board.Length; i++)
{
Transform newGridParent = Instantiate(gridParentObject, startingPos,
transform.rotation);
newGridParent.SetParent(this.transform);
board[i] = newGridParent.GetComponent<Grid>();
board[i] = new Grid(rows, startingPos, gridChildObject,
newGridParent);
startingPos += new Vector3(10, 0, 0);
}
I first instantiate a transform which has the class component, then assign that component from that transform to the array here:
Transform newGridParent = Instantiate(gridParentObject, startingPos, transform.rotation);
board[i] = newGridParent.GetComponent<Grid>();
But when I run the game, the board array is empty, even though I clearly assigned each element in board? What is the error here??
Here's the Grid Class:
[System.Serializable]
public class Grid : MonoBehaviour {
public Transform[] grid;
bool visited;
public Grid (int count, Vector3 startPos, Transform gridObj, Transform parent)
{
grid = new Transform[count];
for(int i = 0; i < grid.Length; i++)
{
Transform newGridChild = Instantiate(gridObj, startPos, Quaternion.Euler(new Vector3(-90, 0, 0)));
newGridChild.SetParent(parent);
startPos += new Vector3(0, 10, 0);
}
}
}
Thanks
Answer by bobisgod234 · Jul 04, 2017 at 12:22 AM
public Grid[] board;
for(int i = 0; i < board.Length; i++)
This will only work if the board array already has elements in it (including null). Did you set the lenth of the 'board' array in the inspector beforehand? If you haven't, this won't do anything, since board.Length will be 0. Add a Debug.Log statement into for for loop to verify if its running or not.
board[i] = newGridParent.GetComponent<Grid>();
board[i] = new Grid(rows, startingPos, gridChildObject,
newGridParent);
You are assigning the Grid component attached to the instance of your "gridParentObject", but then you are overriding that with a new grid anyway, so you will never reference the Grid attached to the instanced transform. This can't be intentional.
public Grid (int count, Vector3 startPos, Transform gridObj, Transform parent)
{
}
You cannot use constructors with Monobehaviours, this script should be producing an error in the console when you try to create a new Grid. Instead, either do your initialization in the Awake/Start function, or alternatively add a public Setup function that takes the same arguments as your constructor, and call that.
I see. I replaced the constructor and did the initialization differently and it works fine for now. Thanks :)
Your answer
Follow this Question
Related Questions
Instantiate object in for loop with array 2 Answers
Instantiating gameobjects in an array / class problem | NullReferenceException: 0 Answers
How can I instantiate a object with a constructor? 3 Answers
adding "X" amount of objects to an array 2 Answers
Object reference not set to an instance of an object with object set, 1 Answer