I keep getting this error after I expanded my spawning class.
NullReferenceException: Object reference not set to an instance of an object
Spawner.SpawnEnemy () (at Assets/Scripts/Spawner.cs:23)
SpawnController.startWave () (at Assets/Scripts/SpawnController.cs:47)
SpawnController.waveController () (at Assets/Scripts/SpawnController.cs:29)
Bellow you will find my Enemy Spawn and SpawnManager classes. Any help would be appreciated, this is driving me bonkers.
using UnityEngine;
using System.Collections;
public class Spawner : MonoBehaviour {
public GameObject Enemy;
GameObject[] objs;
int top;
void Start()
{
objs = GameObject.FindGameObjectsWithTag("Spawn_Point");
top = objs.Length;
// InvokeRepeating("SpawnEnemy", 0.01f, 5.0f);
}
void Update()
{
}
public void SpawnEnemy()
{
Instantiate(Enemy, RandomCircle(objs[Random.Range(0, top)].GetComponent<Transform>().position, 5), Quaternion.identity);
SpawnController.numAlive++;
}
Vector3 RandomCircle(Vector3 center, float radius)
{
float ang = Random.value * 360;
Vector3 pos;
pos.x = center.x + radius * Mathf.Sin(ang * Mathf.Deg2Rad);
pos.y = center.y;
pos.z = center.z + radius * Mathf.Cos(ang * Mathf.Deg2Rad);
return pos;
}
}
using UnityEngine;
using System.Collections;
public class SpawnController : Spawner {
public static int numAlive;
int round;
bool active;
// Use this for initialization
void Start () {
numAlive = 0;
round = 0;
active = false;
InvokeRepeating("waveController", 0.01f, 15.0f);
}
// Update is called once per frame
void Update ()
{
}
void waveController()
{
if(!active)
{
Debug.Log("Active now = true");
active = true;
startWave();
}
else
{
if(numAlive == 0)
{
Debug.Log(" End round ");
active = false;
round++;
}
}
}
using UnityEngine;
using System.Collections;
namespace SpawningFramework
{
public class Enemy : MonoBehaviour
{
public float MaxHealth;
float health;
Vector3 pos;
[HideInInspector]
public bool alive;
void Update()
{
}
/// Called when this enemy has been spawned
void Start()
{
health = MaxHealth;
alive = true;
pos = transform.position;
InvokeRepeating("move", 0.01f, .2f);
}
void move()
{
// transform.Translate(0, 0.025f, 0);
}
/// Called when the player loses
public virtual void OnGameOver()
{
}
void OnTriggerEnter(Collider other)
{
Debug.Log(other.tag);
if (other.tag == ("Player"))
{
Hurt(54);
// Destroy(other.gameObject);
}
}
#region Taking Damage
/// Hurts the enemy and returns if it dies or not
public virtual bool Hurt(float damage)
{
if (!alive)
return false;//exit if dead
health -= damage;//hurt the enemy
if (health < 0.1)
{
alive = false;
SpawnController.numAlive--;
Destroy(this.gameObject);
Destroy(this);
return true;
}
return health < 0.1;//return if this will die
}
#endregion
}
}
void startWave()
{
Debug.Log("Starting wave: " + round);
for(int i = 0; i < (8 + 2 * round); i++)
{
SpawnEnemy();
}
}
}