Spawn prefab on fixed points

Hey guys I am beginner in unity. I’m having a problem on instantiating a prefab. The problem in my code is after I click the accept button in my quest panel or the quest has been accepted, the object will spawn too many (endless loop i don’t know what is called). I successfully spawn objects to their corresponding points. How can I spawn multiple prefab once? I am not sure if it’s to be called on update method.

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

 public class SpawnFlower : MonoBehaviour {

    public GameObject flowers;
    public Transform[] spawnPoints;
    public float spawnTime = 1.5f;

    public int maxCount = 10;

    void Update() {
        //InvokeRepeating ("SpawnFlowers", spawnTime, spawnTime);
        if(QuestManager.questManager.RequestAcceptedQuest(1)) {
            SpawnFlowers ();
        }
    }

    public void SpawnFlowers() {
        List<Transform> freeSpawnPoints = new List<Transform>(spawnPoints);

        for (int i = 0; i < maxCount; i++) {
            if (freeSpawnPoints.Count <= 0)
                return;
            int spawnIndex = Random.Range (0, freeSpawnPoints.Count);
            Transform pos = freeSpawnPoints [spawnIndex];
            freeSpawnPoints.RemoveAt (spawnIndex);
            Instantiate (flowers, pos.position, pos.rotation);
        }
    }
}

It definitely isn’t meant to be in the Update method. You’re calling the function every frame that way. I’m not sure why you’re doing it in the update, but if you find that to be the easiest way, you could have it set a bool once it’s activated and check for that bool each time. Such as…

  using System.Collections;
  using System.Collections.Generic;
  using UnityEngine;
 
  public class SpawnFlower : MonoBehaviour {
 
     public GameObject flowers;
     public Transform[] spawnPoints;
     public float spawnTime = 1.5f;
     private float flowersSpawned = false;
 
     public int maxCount = 10;
 
     void Update() {
         //InvokeRepeating ("SpawnFlowers", spawnTime, spawnTime);
         if(!flowersSpawned && (QuestManager.questManager.RequestAcceptedQuest(1))) {
             SpawnFlowers ();
             flowersSpawned = true;
         }
     }
 
     public void SpawnFlowers() {
         List<Transform> freeSpawnPoints = new List<Transform>(spawnPoints);
 
         for (int i = 0; i < maxCount; i++) {
             if (freeSpawnPoints.Count <= 0)
                 return;
             int spawnIndex = Random.Range (0, freeSpawnPoints.Count);
             Transform pos = freeSpawnPoints [spawnIndex];
             freeSpawnPoints.RemoveAt (spawnIndex);
             Instantiate (flowers, pos.position, pos.rotation);
         }
     }
 }

It’s not pretty but it’ll work. I’d recommend having the quest manager call the function externally, though. You could also look into events and using them to have the SpawnFlowers() function called when the event is fired.