I have a two prefabs (tiles) which have children (squares). I have them in a list where both tiles have a special square named “Skull”. That Skull has variable called Square, which means the child game object itself.
Right now I’m trying to move my player to each tile’s Skull Square with this piece of code:
if(Input.GetKeyDown(KeyCode.S)
{
Vector3 pos = board[Random.Range(0, board.Count)].skull.square.transform.position;
player.transform.position = pos;
}
For the other tile this works well:
But on the other tile the player moves to a wrong position:
I’ve checked that the Skull square has the correct game object attached to it, and the square is positioned correctly on it’s parent.
I suspected that this had something to do with the square being a child object. I’m aware that the inspector shows the child’s position relative to parent, and that transform.position is supposed to give out the global position. When I Debug.Log the pos -variable, it doesn’t give out the same position as in inspector, so therefore transform.position is returning the global position. Also the player’s position is the same as pos. It just doesn’t show up in the correct position visually.
I don’t really understand why the code works for the first tile but not the second one, so if you know where the problem is, feel free to tell me.