I made a GameObject array to keep track of the current enemies for easy referencing, however I cannot get it to work right. It seems like everything is in order, and I can compile and play, but I get the NullReferenceException error when I play.
I am trying to create a script for platforms that the characters can pass through the bottom of.
The three errors I get are
NullReferenceException: Object reference not set to an instance of an object EnemySpawn.Start () (at Assets/Custom Assets/Scripts/Enemy/EnemySpawn.js:18)
NullReferenceException: Object reference not set to an instance of an object Passable Platform.Update () (at Assets/Custom Assets/Scripts/Stage/Passable Platform.js:18)
NullReferenceException: Object reference not set to an instance of an object EnemySpawn.Start () (at Assets/Custom Assets/Scripts/Enemy/EnemySpawn.js:18)
And the Scripts are
------------------EnemySpawn.js--------------------
var spawn : Transform;
var spawnAtStart : boolean = false;
var self : GameObject;
var spawnedBool : boolean = false;
var myClone : GameObject;
var myCloneHealthScript : EnemyHealth;
var cloneBool : boolean = true;
var myCloneFollowScript : EnemyFollow;
var player : GameObject;
var StatisticsScript : Statistics;
function Start(){
player = GameObject.FindWithTag("Player");
StatisticsScript = player.GetComponent(Statistics);
for (var i = 0; i <= StatisticsScript.enemyCount.Length; i++)
{
if(StatisticsScript.enemyCount != GameObject) {
StatisticsScript.enemyCount[i] = gameObject;
return;
}
}
}
function Update () {
if(spawnAtStart == true){
spawnAtStart = false;
spawnedBool = true;
Spawn();
}else{
if(spawnedBool == false){
spawnedBool = true;
Spawn();
}
}
}
function Spawn() {
spawnedBool = true;
myClone = Instantiate(self, spawn.position, Quaternion.identity);
myClone.name = "Clone";
myCloneHealthScript = myClone.GetComponent(EnemyHealth);
myCloneHealthScript.cloneCheck = cloneBool;
myCloneFollowScript = myClone.GetComponent(EnemyFollow);
myCloneFollowScript.cloneCheck = cloneBool;
}
--------------------------Passable Platform.js-------------------------------
var StatisticsScript : Statistics;
//***********************************************************************************************
function Update () {
player = GameObject.FindWithTag("Player");
StatisticsScript = player.GetComponent(Statistics);
/* if the player is made up of more than one collider, you'd
need to iterate over the colliders and use playerBounds.Encapsulate()*/
for(var i = 0; i <= StatisticsScript.enemyCount.Length; i++)
{
enemyBounds = StatisticsScript.enemyCount[i].collider.bounds;
enemyCheck(enemyBounds, StatisticsScript.enemyCount[i]);
}
playerBounds = player.collider.bounds;
playerCheck();
}
//***********************************************************************************************
function playerCheck(){
//check bottom of player to see if it is above the platform
if(playerBounds.min.y >= collider.bounds.max.y)
{
Debug.Log("player is above platform");
//make the platform solid
Physics.IgnoreLayerCollision(8, 10, false);
}else{
Debug.Log("player is below platform");
//turn off platform collision
Physics.IgnoreLayerCollision(8, 10, true);
}
}
//***********************************************************************************************
function enemyCheck(enemyBounds : Bounds, enemy : GameObject){
if(enemyBounds.min.y >= collider.bounds.max.y)
{
Physics.IgnoreCollision(collider, enemy.collider, false);
}else{
Physics.IgnoreCollision(collider, enemy.collider,true);
}
}
----------The relevant script from Statistics.js----------
static var enemyCount : GameObject[];
asked
Apr 16 '12 at 12:52 AM
Dreoh
49
●
7
●
10
●
12
oh, and the lines the errors point to are the for loops
Hard to tell - but the first and obvious question is "Does your Player object have a Statistics behaviour?"
If for some reason they don't, you'd get that exact error.
Yea it does, I checked thoroughly to make sure I had spellings right and everything. Thats why I posted on here
When does enemyCount on Statistics get populated? It is possible it is populated a frame or more after the call in Start() on EnemySpawn
Don't use static variables in Unity. They don't work the way you expect them to.