This is the code I’m using:
//Creates a Matrix with sizexsize
Transform canvas = GameObject.Find("Canvas").transform;
matrix = new GameObject[size, size];
float tileSize = 30.0f;
float corrector = tileSize * size / 2;
for (int i = 0; i < size; i++)
{
for (int j = 0; j < size; j++)
{
float tempX = i * tileSize + Screen.width / 2 - corrector;
float tempY = j * tileSize + Screen.height / 2 - corrector;
matrix[i,j] = (GameObject)Instantiate(prefabTile,
new Vector3(tempX, tempY, 0),
Quaternion.identity, canvas);
//CHECK: For some reasons all the gameObjects are getting
//instantiated in the same spot ( the middle of the canvas )
print(matrix[i, j].transform.position);
matrix[i, j].name = string.Format("[{0},{1}]", i, j);
//This next line solves the problem
matrix[i, j].transform.position = new Vector3(tempX, tempY, 0);
}
}
I’m trying to spawn different button objects set in a panel form in canvas. Before the update ( from 5.6 to 2017.3) it worked as expected. After the update all the prefabs, which are mainly unity buttons, instantiated in the middle of the canvas. I checked that the position vector I was passing was correct and it is. To solve this problem I have to set the position again after the instantiation with the same exact values I passed on to the instantiate function.
I was able to find a momentary solution for this but I’d like to know why this might be happening to have a better understanding.
Thank you in advance!