GameObjects have different positions, but stay in one place
Why do all of my fields are in the same position in the Scene view, but the transform.position is different?
private void GenerateTileMap() {
fields = new GameObject[worldSize, worldSize];
for (int x = 0; x < worldSize; x++) {
for (int y = 0; y < worldSize; y++) {
fields [x, y] = new GameObject ("Field");
fields [x, y].transform.localPosition = new Vector3 (x * fieldSize, 0f, y * fieldSize);
fields [x, y].transform.SetParent (transform);
GameObject newField = BuildTile (10f, "Tile");
newField.transform.SetParent (fields[x, y].transform);
newField.transform.position = new Vector3 (0f, 0f, 0f);
}
}
}
private GameObject BuildTile(float size, string name) {
GameObject newTile = GameObject.CreatePrimitive (PrimitiveType.Cube);
newTile.name = name;
newTile.transform.localScale = new Vector3 (size, 1f, size);
MeshRenderer rend = newTile.GetComponent<MeshRenderer> ();
return newTile;
}
Also, should I convert my arrays to lists? would it be easier to find a tile I need?
I think it has something to do with the hierachy. $$anonymous$$aybe try to set the parent before you set the localPosition?
setting localPosition will offset the object from it's parent. setiing position will move the object to a global position and the localPosition variable will now reflect the new offset from it's parent.
And yeah with Lists you can do alot for finding stuff more easily. Especially with Linq.
Answer by Glurth · Jul 12, 2017 at 05:01 PM
If you want your Tile at the same position as the "field" you parent it to (if not tell me and I'll delete answer):
newField.transform.position = new Vector3 (0f, 0f, 0f);
would be incorrect becuase you assigning to the world position value. These two options would be what you need, I recommend the second (note how it is similar/dissimilar to what you do with the "fields", and THEIR parent).
newField.transform.position = fields[x, y].transform.position;
or
newField.transform.localPosition = new Vector3 (0f, 0f, 0f);
PS: you are storing the built tile in an object named newField- could be a little confusing.
I am trying to make a double tile map. Inside the "Field" can be either another tile map or just a 10 by 10 cube. Not sure if I should keep my fields and their children inside lists or jagged arrays tho.
Your answer
Follow this Question
Related Questions
Does the gameobject brush create tiles when painting on a 2d Tilemap? 1 Answer
Hexagonal Tilemap with 3D GameObject brush. Every 2 rows it is misalligned 0 Answers
How to find the GameObject attached to a Tile? 0 Answers
Tilemap not being synced across network 0 Answers
Can I insert sets of tilemaps into a gameobject and maintain the efficiency of the tilemap system? 0 Answers