can not instantiate for the second time

So i have TileMap Script to create the whole map with the cube that have class on TileType, for the first time the map is generated, and then i want to make a highlight on the cube, by replacing the tiles(cube) with the different one using the highlight() .

TileMap Script

    void Start()
    {
        //Allocate the tiles of the map
        tiles = new int[mapSizeX, mapSizeZ];

        //Create the map
        CreateMap();

        //Generate the map
        GenerateMap();
    }

    void GenerateTiles(int x, int z)
    {
        TileType tt = tileTypes[tiles[x, z]];
        if (tt != null)
            Instantiate(tt.tileVisualPrefab, new Vector3(x, -0.1f, z), Quaternion.identity);
        else
        {
            Debug.LogWarning("tt is empty");
            Debug.Log("On Coordinate " + x + "," + z);
        }
    }

    void GenerateMap()
    {
        for (int x = 0; x < mapSizeX; x++)
        {
            for (int z = 0; z < mapSizeZ; z++)
            {
                GenerateTiles(x, z);              
            }
        }

    }

    void CreateMap()
    {
        //Create the map based on the tiles
        for (int x = 0; x < mapSizeX; x++)
        {
            for (int z = 0; z < mapSizeZ; z++)
            {
                tiles[x, z] = 0;
            }
        }

        //Create the road
        tiles[4, 2] = 1;
        tiles[4, 3] = 1;
        tiles[4, 4] = 1;
        tiles[5, 4] = 1;
        tiles[6, 4] = 1;
        tiles[7, 4] = 1;
        tiles[3, 4] = 1;
        tiles[2, 4] = 1;
        tiles[1, 4] = 1;

        //Create entry/ exit point
        tiles[4, 1] = 3;

        //Assign the bool for each type of tiles
        for(int t = 0; t<=5;t++)
        {
            TileType tt = tileTypes[t];
            isWalkable[t] = tt.isWalkable;
            isDoor[t] = tt.isDoor;
        }
    }

    

    public void highlight(string status, int x, int z)
    {
        if (status == "on")
        {
            temp = tiles[x, z];
            tiles[x, z] = 4;
            GenerateTiles(x, z);
        }
        else
        {
            tiles[x, z] = temp;
            GenerateTiles(x, z);
        }
    }

TileType Class Script

[System.Serializable]
public class TileType
{
    public string name;
    public GameObject tileVisualPrefab;
    public bool isWalkable = false;
    public bool isDoor = false;
    public bool isHighlight = false;
}o Genert

But when i use the highlight() from the other script i triggered got log “tt is empty”, i’ve checked, the string = on and the x and z value is detected and got to GenerateTiles()

what happen? and how do i make the highlight() to instantiate the tiles??

Good day @Molten-Path !!

Well, as you say, you get the “else” that executes the

        Debug.LogWarning("tt is empty");

at line 20 → So, tt is empty → So line 15 is not working fine

     TileType tt = tileTypes[tiles[x, z]];

Try to

Debug.Log (tileTypes[tiles[x, z]])

before line 15, and let’s see what is tring to define as tt. It should be there the problem. Mybe you need to define tt in some other way.

Waiting your respond.

Upvote and respond using @tormentoarmagedoom !

Bye! :smiley:

Thank you all for your helps, i have found a solution.
Because i think the problem resides in the ButtonMove Script (mentioned earlier in the comment), i decides to make isOn bollean variable change the void click to contain only “isOn = true” statement, and add update function on TileMap Script

void Update()
    {
        if(ButtonMove.isOn == true)
        {
            if (isWalkable[tiles[ButtonMove.posX, ButtonMove.posZ + 1]] == true)
            {               
                highlight("on", ButtonMove.posX, ButtonMove.posZ + 1);
            }

            if (isWalkable[tiles[ButtonMove.posX, ButtonMove.posZ - 1]] == true)
            {
                highlight("on", ButtonMove.posX, ButtonMove.posZ - 1);
            }

            if (isWalkable[tiles[ButtonMove.posX + 1, ButtonMove.posZ]] == true)
            {
                highlight("on", ButtonMove.posX + 1, ButtonMove.posZ);
            }

            if (isWalkable[tiles[ButtonMove.posX - 1, ButtonMove.posZ]] == true)
            {
                highlight("on", ButtonMove.posX - 1, ButtonMove.posZ);
            }
        }
    }

also i change the highlight() function a bit

public void highlight(string status, int x, int z)
    {
        if (status == "on")
        {
            Destroy(tileList[x, z]);
            temp = tiles[x, z];
            tiles[x, z] = 4;
            GenerateTiles(x, z);           
        }
    }

now whenever i click the button, the update function will check surrounding player wherever he can move to there or not, and if that is walkable than the highlight() function will remove the original tiles and replace it with the highlight tiles, and it work

so my speculation why i cant do it earlier is maybe because the “TileMap newTiles = new TileMap();” line in the ButtonMove Script creates a whole new TileMap that have the tileTypes empty, so when i want to call something that belongs to tileTypes that i thought still the same as the old one, will return null

if anyone can explain better, you are welcome to do it, thank you :smiley: