Hi,
im a beginner in scripting and i have been following a tutorial
(http://dl.dropboxusercontent.com/u/25260770/UnityLessons/FeuUnity3dLesson12.pdf)
from the unity website and right now im stuck and dont know what to do.The problem is, when i start the game and kill around 3 enemys then the console is giving me the same error message(*Missing Reference Exception The object of type “GameObject” has been destroyed but you are still trying to acces it. Your script your either check if its null or you should not destroy the object.*Line 45) again and again.
can you help me out?
using UnityEngine;
using System.Collections;
public class EnemySpawner : MonoBehaviour
{
static int _livingZombies = 0;
static public void OnEnemyDeath()
{
-- _livingZombies;
}
[SerializeField]
GameObject _enemyToSpawn;
[SerializeField]
float _spawnDelay = 1.0f;
[SerializeField]
int _enemyLimit = 30;
float _nextSpawnTime = -1.0f;
[SerializeField]
LayerMask _spawnLayer;
void Update ()
{
if (Time.time >= _nextSpawnTime _livingZombies < _enemyLimit)
{
Vector3 edgeOfScreen = new Vector3(1.25f, Random.value, 8.0f);
if (Random.value > 0.5f)
{
edgeOfScreen.x = -0.25f;
}
Ray ray = Camera.main.ViewportPointToRay(edgeOfScreen);
RaycastHit hit;
if ( Physics.Raycast(ray, out hit, Mathf.Infinity, _spawnLayer.value))
{
Vector3 placeToSpawn = hit.point;
Quaternion directionToFace = Quaternion.identity;
Instantiate(_enemyToSpawn,placeToSpawn, directionToFace);
_nextSpawnTime = Time.time +_spawnDelay;
++ _livingZombies;
}
}
}
}