How to make object spawn only once?

Hello,

I have been trying to make a random object from a predefined list spawn at a single spawn location. So far this is successful, however I want it to spawn only once and right now it spawns every tick once the condition is met.

The code successfully changes the object to spawn at set intervals. I have tried boolean operations and instantiate but with very little luck. can someone please look at the code and suggest a way to make the object spawn only once.

public GameObject RedPrefab, BluePrefab, GreenPrefab, YellowPrefab; // prefabs to spawn
public float spawnRate = 2.0f;
public float nextSpawn = 1.0f;
int whatToSpawn;

// Update is called once per frame
void Update()
{
    if (Time.time > nextSpawn) // if time is greater than nextSpawn spawn object
    {
        whatToSpawn = Random.Range(1, 5);
        Debug.Log(whatToSpawn);
        nextSpawn = Time.time + spawnRate;
    }

    switch (whatToSpawn) // random range objects to spawn
    {
        case 1:
            Instantiate(RedPrefab, transform.position, Quaternion.identity);
            break;
        case 2:
            Instantiate(BluePrefab, transform.position, Quaternion.identity);
            break;
        case 3:
            Instantiate(YellowPrefab, transform.position, Quaternion.identity);
            break;
        case 4:
            Instantiate(GreenPrefab, transform.position, Quaternion.identity);
            break;
    }
}

}

Hi there. Perhaps you could try using something like this:

     public GameObject RedPrefab, BluePrefab, GreenPrefab, YellowPrefab; // prefabs to spawn
     public float spawnRate = 2.0f;
     public float nextSpawn = 1.0f;
     int whatToSpawn;
     private bool hasSpawned = false;

     // Update is called once per frame
     void Update()
     {
         if (Time.time > nextSpawn) // if time is greater than nextSpawn spawn object
         {
             whatToSpawn = Random.Range(1, 5);
             Debug.Log(whatToSpawn);
             nextSpawn = Time.time + spawnRate;
         }
         switch (whatToSpawn) // random range objects to spawn
         {
             if(hasSpawned == false)
             {
                 case 1:
                     Instantiate(RedPrefab, transform.position, Quaternion.identity);
                     hasSpawned = true;
                     break;
                 case 2:
                     Instantiate(BluePrefab, transform.position, Quaternion.identity);
                     hasSpawned = true;
                     break;
                 case 3:
                     Instantiate(YellowPrefab, transform.position, Quaternion.identity);
                     hasSpawned = true;
                     break;
                 case 4:
                     Instantiate(GreenPrefab, transform.position, Quaternion.identity);
                     hasSpawned = true;
                     break;
             } else
             {
                 break;
             }
             
         }
     }

The boolean set here will return true once an object has spawned, which should prevent the code from executing anymore. You can then reset the boolean to false once you want another object to spawn. Honestly, not 100% sure if this will work as I haven’t tested it myself, but it’s a sugggestion anyway. Good luck!