My variables arent updating accordingly after I instantiated them

I am trying to instantiate the rock blocks in a grid style and save their information in another script via public ints. The blocks spawn perfectly as intended but the information doesn’t save correctly. I set the default values on the prefabs of the gameObjects that I create to 0 and isDead to false. The first 3 gameobject “clones” spawn with all the information to 0 and after the it saves 1 or 2 correctly (might be luck) but then it becomes pretty much random. Any help would be greatly appreciated!

The script that saves the positions :

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

//Save the spawn position in the array of all the tiles
public class TileInformation : MonoBehaviour
{
    public int PositionInLayer;
    public int xPositionInLayer;
    public int yPositionInLayer;
    public int layerDept;
    public bool isDead = false;


}

The script that creates the prefabs (Only pasting the part that isn’t working as intended)

    //Values to make the grid
    public int amountOfLayers;
    public int sideSize;
    public int heightSize;
    private int layerCounter = 0;


    //The tiles to spawn
    public GameObject SandStoneTile;
    public GameObject LightRockTile;
    public GameObject RockTile;
    public GameObject SandFloor;

    //Gets us the initial posiiton
    public Transform initialPosition;

    //Get all the selected tiles
    GameObject[,] allTiles;


    private void Start()
    {

        //while we haven't ocupied all the layers
        int blocksSide;
        int blocksHeight;
        int arrayCounter;

        //Fill all the layers with blocks
        while (layerCounter != amountOfLayers)
        {
            blocksSide = 0;
            blocksHeight = 0;
            arrayCounter = 0;
            
            //while we havent ocupied all block in a certain layer
            
         
            while (arrayCounter != heightSize * sideSize)
            {

                //Get random gameObjectTile
                GameObject SelectedObject = RandomPicker();

                //Instantiate our random Object
                Instantiate(SelectedObject, new Vector3(initialPosition.position.x + blocksSide, initialPosition.position.y + blocksHeight * -1, 1 * layerCounter),Quaternion.identity);

                //Put the newly created block into an array. 
                allTiles[layerCounter ,arrayCounter] = SelectedObject;

               //I used this to see if the numbers are set correctly which they are!
                Debug.Log("Counter:" + arrayCounter + "GameObject:" + SelectedObject.name + "Positionx:" + blocksSide + "Position y :" + blocksHeight);

                //This will save where in, another script, a determained clone gameobject is 
                TileInformation TilePosition = SelectedObject.GetComponent<TileInformation>();
                TilePosition.layerDept = layerCounter;
                TilePosition.xPositionInLayer = blocksSide;
                TilePosition.yPositionInLayer = blocksHeight;
                TilePosition.PositionInLayer = arrayCounter;


                //Increment to the next position
                arrayCounter++;
                blocksSide++;

                //if we get to the last position in the grid, we go down if it isnt the last position
                if (blocksSide == sideSize && arrayCounter != heightSize * sideSize)
                {
                    blocksSide = 0;
                    blocksHeight++;
                }
               
            }
            layerCounter++;
        }
}

//To pick a random Gameobject Note : Yes I know it would be better with a switch case :P
  GameObject RandomPicker()
    {
        float value = Random.Range(0f, 1f);

        //60% chance of a Sand Tile, 30% chance of a lightRock Tile and 10% of a Rock Tile
        if (Dificulty == "Easy")
        {
            if (value <= 0.6f) return SandStoneTile;
            else if (value > 0.6f && value <= 0.9f) return LightRockTile;
            else return RockTile;
        }

        //45% chance of a Sand Tile, 30% chance of a lightRock Tile and 25% of a Rock Tile
        else if (Dificulty == "Medium")
        {
            if (value <= 0.45f) return SandStoneTile;
            else if (value > 0.45f && value <= 0.75f) return LightRockTile;
            else return RockTile;
        }

        //30% chance of a Sand Tile, 30% chance of a lightRock Tile and 40% of a Rock Tile
        else if (Dificulty == "Hard")
        {
            if (value <= 0.3f) return SandStoneTile;
            else if (value > 0.3f && value <= 0.6f) return LightRockTile;
            else return RockTile;
        }
        //if something goes wrong 
        return SandFloor;
    }

Your SelectedObject is not a reference to a new GameObject, it is a reference to the prefab. So you are creating the prefab and then you are just “saving” the values on the Prefab, not the instantiated object. You need to get a reference to the instantiated object instead:


GameObject SelectedObject =  Instantiate(RandomPicker(), new Vector3(initialPosition.position.x + blocksSide, initialPosition.position.y + blocksHeight * -1, 1 * layerCounter),Quaternion.identity);