大家好,我是团结的初学者。我在实例化预制件时遇到问题。我的代码中的问题是,在我单击任务面板中的接受按钮或任务被接受之后,该对象会产生太多(无限循环我不知道什么叫)。我成功地将对象生成到它们的相应点。如何一次产生多个预制件?我不确定是否要在更新方法上调用它。
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;
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);
}
}
}