Adding a new GameObject to an Array without overriding the others
Hello,
my floor is made of different Tiles which i want to store in an array. The only problem is, that the tiles that i add to my array overwrite the other Tiles. If I instantiate them directly, their position is right. But if i store them in an array and instantiate every single Tile in the Array, they end up having the same properties. So i basically instantiate the same GameObject over and over again. Heres The Code:
public int Length;
public int Width;
public GameObject Tile;
public GameObject[] floorTiles;
int floorTileIndex =0;
private void GenerateTheFloor()
{
//Loop for the FloorLength
for (int i = 0; i < Length; i++)
{
floorTiles[floorTileIndex- 1] = Tile;
TileSkript tSkript = floorTiles[floorTileIndex- 1].GetComponent<TileSkript >();
tSkript .YPos = i;
tSkript .XPos = 0;
bodenAusFliesen[FliesenIndex - 1].transform.SetPositionAndRotation(new Vector3(tSkript .XPos, 0, tSkript .YPos), new Quaternion(0, 0, 0, 0));
floorTileIndex++;
#endregion Länge
//Loop for the width
for (int a = 0; a < width-1; a++)
{
floorTiles[floorTileIndex- 1] = Tile;
TileSkript tSkript = floorTiles[floorTileIndex- 1].GetComponent<TileSkript >();
tSkript .YPos = i;
tSkript .XPos = 0;
bodenAusFliesen[FliesenIndex - 1].transform.SetPositionAndRotation(new Vector3(tSkript .XPos, 0, tSkript .YPos), new Quaternion(0, 0, 0, 0));
floorTileIndex++;
}
}
}
Your answer
Follow this Question
Related Questions
Instantiate players in Photon. Error: Index was outside the bounds of the array. 1 Answer
Getting the element that has the same element number on another list ? 2 Answers
How to create an array of clones for a seperate Save/Load script 0 Answers
Instantiate and remove objects according to the number of objects in an Array 1 Answer
I have trouble understanding arrays and enums. When and how? 0 Answers