Problem with parent gameObject not where it should be after adding childs
I am playing around with sprite chunks in Unity3D for a 2D game.
One chunk has a sprite prefab which contrains a scale, a type and a UnitySprite for displaying the image. In addition each chunk has an array TileSprite[ , ] for the single sprites.
The Start-method for the Chunk looks like this
transform.position = new Vector3(position.x, position.y, -1);
transform.parent = parent.transform;
InitializeSpriteMap();
// Init here with default values for rendering!
for (int x = 0; x < ChunkSize; x++)
{
for (int y = 0; y < ChunkSize; y++)
{
TileSprite sprite = (Instantiate(tilePrefab) as GameObject).GetComponent<TileSprite>();
// todo Take valiue form generated terrain/Chunk/world
sprite.Instantiate("Grass", sprites[TileType.Gras], TileType.Gras, new Vector2(x, y), transform);
tiles[x, y] = sprite;
}
}
The Instantiate-Part for the TileSprite looks like this:
public void Instantiate(string name, Sprite sprite, TileType type, Vector2 position, Transform parent)
{
SetImage(sprite);
var bounds = tileSprite.bounds;
transform.localScale = new Vector3(tileSize / bounds.size.x, tileSize / bounds.size.y, 1);
bounds = tileSprite.bounds;
tileName = name + position.ToString();
transform.name = tileName;
this.type = type;
SetPosition(position*tileSize);
transform.parent = parent;
}
TileSprite -> SetPosition
public void SetPosition(Vector2 position)
{
this.position = position;
transform.position = new Vector3(position.x, position.y, -1);
}
Soo what is the problem... The TileSpirtes are added correctly without a gap between adjacent TileSprites.
The problem is that the position of the Chunk is vector3(0,0,0) before adding any TileSprites and after added the tiles it is still Vector3(0,0,0) but in the scene the Chunk has moved!!!!!! It is not in Camera (0,0,0) anymore.
The Camera is placed in Vector3(0,0,0). I am using center and global as setup for Unity-Editor. I want to add more Chunks next to each other later, but I want the one chunk added to be at (0 ,0 ,0)
FYI: When I add more chunks, the Tiles for each chunk are at the same position the position oif the chunk is changing but only in numbers not visible....
Can anyone explain to me why this is happening and what I can do to fix this? I was reading what I can find on Google, and played around for 2 evenings, but didn't came up with a solution...
One more thing: Each chunk is a child of an empty "world" gameobject!
If you need more code, just let me know, I will post everything you need to know, but I didn't want to post too much code.
Your answer
Follow this Question
Related Questions
How to get children from a Parent and not get the children of the children too 4 Answers
How to ungroup objects to its parent? 1 Answer
How do I set the pivot to the exact position as parent object? 0 Answers
Rotation problem in contructing arm with parent / child reloation (forearm move way faster) 0 Answers