Hello again. I have looked at various other questions but I am still having trouble with this. I am trying to instantiate an prefab every time it is deleted. I used OnMouseDown() as the way to set the item inactive but I have decided on just deleting the item and I want to reset any one of the prefab counts in the IEnumerator so the selected prefab type can respawn. Can someone please tell me what I am missing? My goal is to instantiate a new prefab such as ItemPrefab when the last one is deleted while it keeps count on how many are on screen so I won’t have more than 1 prefab of the same type on the screen at a time.
public Transform moveSpot;
// It is an item that you specify
private float minX;
// It is an item that you specify
private float maxX;
// It is an item that you specify
private float minY;
// It is an item that you specify
private float maxY;
// It is an item that you specify
[SerializeField]
private GameObject ItemPrefab;
// This is what you use to spawn items ---- You may need to delete.
public GameObject ItemPrefab2;
// This is what you use to spawn items ---- You may need to delete.
//public GameObject ItemPrefab3;
// This is what you use to spawn items ---- You may need to delete.
List<GameObject> prefabList = new List<GameObject>();
//List<GameObject> prefabList2 = new List<GameObject>();
//public int maxItem = 1;
int lastValue;
void Start()
{
// The start of the script.
speed = 1f;
startWaitTime = 3f;
waitTime = startWaitTime;
minX = -1.6f;
maxX = 1.6f;
minY = -4f;
maxY = 4f;
prefabList.Add(ItemPrefab);
prefabList.Add(ItemPrefab2);
//prefabList.Add(ItemPrefab3);
startSpawnTime = 1f;
// 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.
StartCoroutine(SpawnRoutine());
}
void Update()
{
// After the initial work has been completed.
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.
}
}
}
IEnumerator SpawnRoutine()
{
Vector2 spawnPos = Vector2.zero;
int spawnCount = 0;
int spawnCount1 = 0;
while (true) {
//spawnCount <= 0 && spawnCount1 <= 0
//yield return new WaitForSeconds(3f);
spawnPos.x = Random.Range(0f, 0f);
spawnPos.y = Random.Range(0f, 0f);
int prefabIndex = UnityEngine.Random.Range(0, 3);
if (prefabIndex == 1 && spawnCount <= 0)
{
Instantiate(ItemPrefab, spawnPos, Quaternion.identity);
spawnCount++;
}
else if (prefabIndex == 2 && spawnCount1 <= 0)
{
Instantiate(ItemPrefab2, spawnPos, Quaternion.identity);
spawnCount1++;
}
yield return new WaitForSeconds(3f);
//reason why this is working
}
}
public void OnMouseDown()
{
gameObject.SetActive(false);
}
}