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);
}
}
}