only spawning one unique object in a 2d based stage spawner ?

As the title suggests I am using a tile spawner script to spawn a grid (3x3) onwards. What I want is that only ONE of the tiles must be of “green” color
My spawner code:

public void SpawnStage()
    {
        //Basically only edit stage length for rows and columns
        ColumnLength = stageLength;
        RowHeight = stageLength;
        stageUnit = new GameObject[ColumnLength, RowHeight];
        for (int i = 0; i < ColumnLength; i++)
        {
            for (int j = 0; j < RowHeight; j++)
            {

                stageUnit[i, j] = (GameObject)Instantiate(floor, new Vector3(i, 0, j), Quaternion.identity);
            }

        }

as to how my object(s) get their properties is that i have used a random . range 0 to 100 to have probability as in life up has the least (0 to 5 ) and normal tiles have the most (40 to 90) i think.

  if (i >40)
        {
            //white is normal tile
            this.gameObject.name = "White";
            GetComponent<Renderer>().material.color = Color.white;
            isNormalTile = true;
        }
         if (i>80)
        {
            if (greenTileSpawned == false)
            {
                //Green is stage clear tile
                this.gameObject.name = "Green";
                GetComponent<Renderer>().material.color = Color.green;
                isGreenTile = true;
                greenTileSpawned = true;
            }
        }

so to recap what I want is that once one single green has spawned no more should be green. Looking for any suggestions on how to approach this.

Set a boolean when you place the green tile.

Better:

  • set all tiles white
  • go back and set ONE random tile green
1 Like

approach 2 can work because tiles need to assigned values as soon as stage is created. but if i spawn normal tiles then what i can do is :
basically after script 1 / spawner script :

Gameobject[ ] whitetile = find all with name white
int i where i = random.range (0, whitletile.length)
{
i.color = green
i.name = green
}
and then in player i can set the trigger to stage spawner for if collider with name green.
its past 12 here but i promise i will try this tomorrow and get back to you thanks for replying

Another way is to choose the x,y of the green one:

int greenx = Random.Range( 0, ColumnHeight);
int greeny = Random.Range( 0, RowHeight);

Then as you iterate i,j simply check them against greenx,greeny

// inside the two for() loops:

if (i == greenx && j == greeny)
{
   // green
}
else
{
   // white
}

“ColumnHeight” and “RowHeight” are poor names… they cannot both be Height, right?

How about NumColumns and NumRows ?

Basically there are 4 colors at the moment “blue” for double jump , yellow for -1 hp and red for instant game over and green for increasing stage length. the code is built to scale for more.
could be done i had a deadline on this project so was trying to complete the functionality once its done i will try to refactor properly as much as i can its a relatively short project so it wont take that long
edit: i accidentally put some of my views in the code