im trying to make it to where zombie spawn at empty gameobjects but i keep getting the error:
NullReferenceException: Object reference not set to an instance of an object
zombieSpawn.OnTriggerEnter (UnityEngine.Collider other) (at Assets/scripts/zombieSpawn.js:17)
any help would be appreciated. Here is my code
var Zombie : Transform;
var ZombieSpawn1 : Transform;
var ZombieSpawn2 : Transform;
var ZombieSpawn3 : Transform;
var ZombieSpawn4 : Transform;
var ZombieSpawn5 : Transform;
public var ZombiesSpawned = 0;
function OnTriggerEnter (other : Collider) {
if (other.tag == "Player")
{
if (ZombiesSpawned < 5)
{
Instantiate(Zombie, ZombieSpawn1.position, Quaternion.Identity);
ZombiesSpawned += 1;
}
}
}
Have you dragged blank a transform reference into the ZombieSpawn1 field on the game object where this script resides? Have you supplied a Zombie prefab to spawn?
I don’t see any code where you setting zombie and zombiespawn1.
Just verify you have them set and it should clear the null ref error. Zombie normally needs to be a GameObject not just the Transform.
You can add public to them. To set the spawn locations via the inspector.
public var Zombie : GameObject;
public var ZombieSpawn1 : Transform;
public var ZombieSpawn2 : Transform;
public var ZombieSpawn3 : Transform;
public var ZombieSpawn4 : Transform;
public var ZombieSpawn5 : Transform;
public var ZombiesSpawned = 0;
function OnTriggerEnter (other : Collider) {
if (other.tag == "Player")
{
if (ZombiesSpawned < 5)
{
if(Zombie != null)
{
Instantiate(Zombie, ZombieSpawn1.position, Quaternion.Identity);
ZombiesSpawned += 1;
}else
{
Debug.Log("Zombie is null - set it via code or in unity");
}
}
}
}
I ran your code and it didn’t say “Zombie is null - set it via code or in unity” in the console and got the same error
im using a prefab for the zombie and empty game objects for the spawn points if that helps
Yeah, that is odd. That shouldn’t be a null error. As far as I know, Quaternion doesn’t implement Identity, so you should have received a different error.