- Home /
Instantiated object coordinates from code aren't the same as the coordinates from Inspector?,Inspector Coordinates not the same as GameObject.Find Coordinates?
So I have this little code here:
private static readonly int Rows = 10;
private static readonly int Cols = 10;
private readonly int[,] Matrix =
{
{ 1, 0, 1, 1, 1, 1, 1, 1, 1, 1 },
{ 1, 0, 1, 1, 1, 1, 0, 1, 1, 1 },
{ 1, 0, 0, 1, 1, 1, 0, 0, 0, 1 },
{ 1, 1, 0, 0, 1, 1, 1, 0, 1, 1 },
{ 1, 1, 1, 0, 1, 0, 1, 0, 1, 1 },
{ 1, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
{ 1, 0, 1, 1, 1, 1, 1, 0, 1, 1 },
{ 1, 0, 0, 1, 1, 1, 1, 0, 1, 1 },
{ 1, 1, 0, 0, 1, 1, 0, 0, 1, 1 },
{ 1, 1, 1, 0, 1, 1, 1, 1, 1, 1 }
};
public GameObject FloorTile;
public GameObject Floor;
void CreateFloor(int i, int j,int floor)
{
float size = FloorTile.transform.localScale.x;
if (floor == 1)
{
GameObject FloorObject = Instantiate(FloorTile, new Vector3(i * size, 0, j * size), Quaternion.identity);
FloorObject.name = "Tile[" + i + "][" + j + "]";
FloorObject.transform.SetParent(Floor.transform);
}
if(floor==0)
{
GameObject FloorObjectPath = Instantiate(FloorTile, new Vector3(i * size, 0, j * size), Quaternion.identity);
//Debug.Log(i+" "+j);
FloorObjectPath.name = "TilePath[" + i + "][" + j + "]";
FloorObjectPath.transform.SetParent(Floor.transform);
}
}
void Start()
{
for (int i = 0; i < Rows; i++)
{
for (int j = 0; j < Cols; j++)
{
CreateFloor(i, j,Matrix[i,j]);
}
}
Debug.Log(GameObject.Find("Tile[2][4]").transform.position);
}
I used Debug.Log(GameObject.Find("Tile[2][4]").transform.position);
to see the position of my tile but when I check the console for the position of my Tile[2][4]
it says 6.0,0.0,12.0
or something. When I look in the Inspector its -50.239422, 2.235324, -23,234342
(not exactly this, but you got the point).
Now my question is why are those coordinates different and how can I make them be equal?
Also I used "Instantiate to spawn another object near my "Floor" and its at the same spot as 1 tile of the Floor, but they have different coordinates in the Inspector... I'm totally confused.
Answer by sacredgeometry · Dec 10, 2020 at 07:34 PM
There are two sorts of coordinate space in unity (well three including screen space). World and local. One is relative to the world origin and the other is relative to your immediate parent (all the way up to world through your descendants)