Hello! I am having trouble making my script work in a way that is stops the instantiate request from spawning duplicate prefabs. I just want to check if a particular prefab is already present and if it is, stop the instantiate request from trying to load it again. That way only one prefab type is present and others can be loaded. Is there something I am missing?
public Transform moveSpot;
private float minX;
private float maxX;
private float minY;
private float maxY;
public GameObject ItemPrefab;
public GameObject ItemPrefab2;
public GameObject ItemPrefab3;
List<GameObject> prefabList = new List<GameObject>();
private float speed;
private float waitTime;
private float startWaitTime;
private float SpawnTime;
private float startSpawnTime;
void Start()
{
// The start of the script.
speed = 1f;
startWaitTime = 3f;
waitTime = startWaitTime;
minX = -1.6f;
maxX = 1.6f;
minY = -4f;
maxY = 4f;
startSpawnTime = 3f;
// This is what you use to spawn items ---- You may need to delete.
SpawnTime = startSpawnTime;
// This is what you use to spawn items ---- You may need to delete.
moveSpot.position = new Vector2(Random.Range(minX, maxX), Random.Range(minY, maxY));
// Basically, the starting location is randomized based on the x and y ranges you specify.
}
void Update()
{
transform.position = Vector2.MoveTowards(transform.position, moveSpot.position, speed * Time.deltaTime);
// Basically, the updating location is randomized based on the x and y ranges you specify and the speed you specified it to move.
if (Vector2.Distance(transform.position, moveSpot.position) < 0.2f)
{
// Basically, it wants to make sure the moving items is close to the updated MoveSpot position.
if (waitTime <= 0)
{
moveSpot.position = new Vector2(Random.Range(minX, maxX), Random.Range(minY, maxY));
waitTime = startWaitTime;
// Basically, if the time to move is less than 0, the MoveSpot will move to a new location and start the countdown all over again.
}
else
{
waitTime -= Time.deltaTime;
// Otherwise, the item to be move will remain in place until it is time to move.
}
if (SpawnTime <= 0)
{
prefabList.Add(ItemPrefab);
prefabList.Add(ItemPrefab2);
prefabList.Add(ItemPrefab3);
int prefabIndex = UnityEngine.Random.Range(0, 3);
Instantiate(prefabList[prefabIndex]);
SpawnTime = startSpawnTime;
// This is what you use to spawn items ---- You may need to delete.
}
else
{
SpawnTime -= Time.deltaTime;
}
}
}
}