x


Getting NullReferenceException? info inside

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[];
more ▼

asked Apr 16 '12 at 12:52 AM

Dreoh gravatar image

Dreoh
49 7 10 12

oh, and the lines the errors point to are the for loops

Apr 16 '12 at 01:02 AM Dreoh

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.

Apr 16 '12 at 03:48 AM IgnoranceIsBliss

Yea it does, I checked thoroughly to make sure I had spellings right and everything. Thats why I posted on here

Apr 16 '12 at 03:49 AM Dreoh

When does enemyCount on Statistics get populated? It is possible it is populated a frame or more after the call in Start() on EnemySpawn

Apr 16 '12 at 04:38 AM hijinxbassist

Don't use static variables in Unity. They don't work the way you expect them to.

Apr 16 '12 at 05:31 AM syclamoth
(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

It's tough to say without seeing more, but are you instantiating your array, and if so is it happening before it's being used by these scripts? You define var enemyCount which is a builtin array that needs to be declared using:

enemyCount = new GameObject[<size>];

Builtin arrays are a fixed size defined at that point and can't be resized. If that's not done before you begin accessing enemyCount you'll get an error like this.

more ▼

answered Apr 16 '12 at 05:40 AM

Atrius gravatar image

Atrius
152 1 3

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x5272
x422
x276
x159
x97

asked: Apr 16 '12 at 12:52 AM

Seen: 479 times

Last Updated: Apr 16 '12 at 01:08 PM