I’ve got a script for spawning enemies but I’ve run into a slight snag on the finishing touches. I’ve limited the spawn so that only a certain amount can be spawned at a certain time. The thing is, I want to be able to spawn more once the enemies I’ve already spawned have been destroyed.
Spawner Script:
public GameObject[] spawnObject;
public float radius = 1.0f;
public float minSpawnTime = 1.0f;
public float maxSpawnTime = 10.0f;
public bool constantSpawn = false;
public Transform spawnPosition;
Vector3 randomLocation;
public int SpawnCount = 0;
public float LookAtDistance = 10.0f;
public float Distance;
public Transform Target;
public float Damping = 5.0f;
private Quaternion rotation;
void Start()
{
Target = GameObject.FindGameObjectWithTag ("Player").transform;
}
void Update(){
Distance = Vector3.Distance (Target.position + Target.transform.forward, transform.position);
if (Distance < LookAtDistance)
{
LookAt();
}
}
void OnTriggerEnter(Collider Player)
{
Invoke("SpawnEnemy", Random.Range(minSpawnTime,maxSpawnTime));
}
void OnTriggerExit(Collider Player)
{
CancelInvoke ("SpawnEnemy");
}
void SpawnEnemy ()
{
randomLocation = Random.insideUnitSphere * radius;
float spawnRadius = radius;
int spawnObjectIndex = Random.Range(0,spawnObject.Length);
//transform.position = Random.insideUnitSphere * spawnRadius;
Instantiate(spawnObject[spawnObjectIndex],spawnPosition.position + randomLocation,spawnObject[spawnObjectIndex].transform.rotation);
SpawnCount ++;
if (constantSpawn == true && SpawnCount <= 5)
{
Invoke ("SpawnEnemy", Random.Range (minSpawnTime,maxSpawnTime));
}
//else
//{
// CancelInvoke("SpawnEnemy");
//}
}
void LookAt(){
rotation = Quaternion.LookRotation (Target.localPosition - transform.position);
transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * Damping);
}
public void SpawnDown(int reduction){
SpawnCount -= reduction;
}
}
Script that should reduce the spawn:
public class Leech_AI_Test : MonoBehaviour {
public float MaxDistance = 10.0f;
public float MoveSpeed = 5.0f;
public float MinDistance = 0.0f;
public Transform Target;
public float LookAtDistance = 10.0f;
public float Distance;
public float Damping = 5.0f;
private Quaternion rotation;
public bool parenting = false;
private PlayerHealth_Test playerHealth;
private float nextDam;
//private Bank rolling;
public bool rolling = false;
public float DamDelay = 2.0f;
private Leech_Mother_AI_Test spawnCount;
void Start (){
Target = GameObject.FindGameObjectWithTag ("Player").transform;
playerHealth = Target.GetComponent<PlayerHealth_Test>();
parenting = false;
// rolling = Target.GetComponent<Bank>();
nextDam = Time.time + DamDelay;
spawnCount = Target.GetComponent<Leech_Mother_AI_Test>();
}
void OnTriggerEnter(Collider Player)
{
transform.parent = GameObject.FindGameObjectWithTag("Player").transform;
parenting = true;
}
void Update () {
Distance = Vector3.Distance (Target.position + Target.transform.forward, transform.position);
if (Distance < LookAtDistance)
{
LookAt();
}
if (Distance > MinDistance && Distance < MaxDistance && parenting == false)
{
follow ();
}
if (rolling == true)
{
parenting = false;
GameObject.Destroy(this.gameObject);
spawnCount.SpawnDown(1);
}
if (parenting == true && nextDam <= Time.time)
{
nextDam = Time.time + DamDelay;
playerHealth.CurrentHealth -= 1.0f;
}
if (parenting == true && playerHealth.CurrentHealth <= 0)
{
parenting = false;
}
}
void LookAt(){
rotation = Quaternion.LookRotation (Target.localPosition - transform.position);
transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * Damping);
}
void follow(){
transform.Translate(Vector3.forward * MoveSpeed * Time.deltaTime);
}
}
I’ve already called a variable from another script before and learned that I could do the same with a method so I tried to do that with this
spawnCount = Target.GetComponent();
spawnCount.SpawnDown(1);
But I get this error:
NullReferenceException: Object reference not set to an instance of an object
Leech_AI_Test.Update () (at Assets/Test Scripts/Leech_AI_Test.cs:63)
and I have no idea what to do. I did nothing different than when I called the value from another script (in fact the first time I did this I tried to simply pull the SpawnCount variable instead of a method). You can actually see the other time I pulled a value in the script with playerHealth.
I don’t really have a clue what that error means (especially since it didn’t occur before) and any help would be appreciated.