using UnityEngine;
using System.Collections;
[System.Serializable]
public class Area
{
public float xMin, xMax, zMin, zMax;
}
[System.Serializable]
public class SpawnTimes
{
public float TimeFromStart, Rate;
}
public class Spawn : MonoBehaviour
{
public Area area;
public SpawnTimes time;
public GameObject ball;
public float MaxEnemies = 20.0f;
public float DestroyTime;
public int enemy = 0;
bool CanSpawn = true;
void Start()
{
//StartCoroutine (WaitSpawn());
//CanSpawn = true;
}
/*void Update()
{
if (enemy > MaxEnemies)
{
CanSpawn = false;
}
Else
{
CanSpawn = true;
}
if(CanSpawn == true)
{
Invoke("WaitSpawn",0);
CanSpawn = false;
}
}*/
IEnumerator WaitSpawn()
{
yield return new WaitForSeconds (time.TimeFromStart);
while (enemy < MaxEnemies)
{
float randomX = Random.Range (area.xMin, area.xMax);
float randomZ = Random.Range (area.zMin, area.zMax);
Instantiate (ball, new Vector3 (randomX, 5.0f, randomZ), Quaternion.Euler (0.0f, 0.0f, 0.0f));
enemy++;
yield return new WaitForSeconds (time.Rate);
}
}
}
How are we supposed to help you make it work the way you want it to, if you don’t say what you want it to do?
You should probably specify how you expect it to work.
There is a few comment things and each of them are different ways that I have tried it. One without the section with the update method
That still doesn’t tell us what you’re trying to accomplish with the script, or how its current behavior differs from what you’re trying to do.
Oh ya, So I want it to spawn them when it is under a certain number but stop when it gets there, but when the number drops below that number than it will start again
There is going to be an enemy and they are going to have health, when they die I want the number to drop and spawn another one after a few seconds, and only a few at a time
Right now it either does not spawn or it just stops and never starts again.
I’m guessing it stops and never starts again because there’s nowhere that you decrease “enemy” when an enemy dies. There are a number of ways to do this - one of the simpler ones would be, just before that while loop, set enemy like this:
enemy = FindObjectsOfType<YourEnemyScriptName>().Length;
This will count how many of those enemy objects exist, and set enemy to that.
BTW, MaxEnemies should be an int, not a float.
Thanks I will try that
The script is saying that my EnemyScript does not contain a definition
Nevermind… I was using FindObjectOfType not FindObjectsOfType
So right now it works for adding to the enemy but it wont start up again
right now it looks like:
while (enemy < MaxEnemies)
{
enemy = FindObjectsOfTypes<DestroyByTime>().Length;
float randomX = Random.Range ( area.xMin, area.xMax )
float randomZ = Random.Range ( area.zMin, area.zMax )
Instantiate (ball, new Vector3 (randomX, 5.0f, randomZ), Quaternion.Euler (5.0f,5.0f,5.0f));
yield return new WaitForSeconds (time.Rate);
}
this is what worked the best from the help that you gave
It won’t start up again because there’s just one loop, and you exit that loop as soon as it fills up once.
You may want to put another while (true) { loop outside the existing loop - that way it does the whole thing over again after it finishes. Make sure you have a yield return something in the outer loop as well, though, or Unity will freeze (infinite loop)!
So what would that look like