AI Path-finding error

I have been working on this path finding script for my android app. I’m getting an error that i can seem to fix. Can anyone help? Thanks.

Gates are triggers with rigidbodys at the edge of an area. Areas are also triggers with rigidbodys. The AI has a CharacterController for movement, and a collider acting as a trigger for the path.

Error:

NullReferenceException: Object reference not set to an instance of an object
Boo.Lang.Runtime.RuntimeServices.CheckNumericPromotion (IConvertible convertible)
Boo.Lang.Runtime.RuntimeServices.CheckNumericPromotion (System.Object value)
Boo.Lang.Runtime.RuntimeServices.UnboxInt32 (System.Object value)
AI_Controller.Update () (at Assets/AI_Controller.js:46)

AI movement - The error is coming from this.

#pragma strict
var health : float = 100f;
var deathEffect : GameObject;

var currentArea : GameObject;
var player : GameObject;
//@HideInInspector
//var playerScript : GameObject;
var playerArea : GameObject;
var targetDoor : GameObject;
var currentPathLength : float;
@HideInInspector
var startDelay : int = 5;
var controller : CharacterController;
function Start () {
currentPathLength = Mathf.Infinity;
player = GameObject.FindGameObjectWithTag("Player");
//playerScript = player.GetComponent(Player_Move);
startDelay = 5;
controller = gameObject.GetComponent(CharacterController);
}

function Update () {



  if(health <= 0) {
    if(deathEffect != null)
      Instantiate(deathEffect, transform.position,transform.rotation);
    Destroy(gameObject);
  }
  
  if(startDelay <= 0) {
  
    var playerScript = player.GetComponent(Player_Move);
    playerArea = playerScript.currentAreaP;
    
    for(var doorChecking : GameObject in currentArea.GetComponent(AI_Area).gates) {
    
      Debug.Log("a");
      
      for(var i = 0; i < doorChecking.GetComponent(AI_Gate).areas.length-1; i++) {
      
        if(doorChecking.GetComponent(AI_Gate).areas[i] == playerArea) 
        
          var gates : int = doorChecking.GetComponent(AI_Gate).numberOfGates[i];
          
          Debug.Log(gates);
          
          if(gates < currentPathLength) {
          
            targetDoor = doorChecking;
            
            currentPathLength = gates;
          }
      }
    }
  } else {
  startDelay--;
  }
  
  currentPathLength = Mathf.Infinity;
  
      //Move the AI
      var moveDir = Physics.gravity;
      if(targetDoor != null) {
        var targetRot = Quaternion.LookRotation(Vector3(targetDoor.gameObject.transform.position.x,0,targetDoor.gameObject.transform.position.z) - Vector3(transform.position.x,0,transform.position.z));
        transform.rotation = Quaternion.Slerp(transform.rotation, targetRot, Time.deltaTime * 5);
        moveDir += transform.forward;
      }
      controller.Move(moveDir *2* Time.deltaTime);
      
}

function OnTriggerEnter(other : Collider) {
  if(other.tag == "AI_area")
    currentArea = other.gameObject;
}

AI_area

#pragma strict


var gates = new Array();

//Get touching gates
function OnTriggerEnter(other : Collider)  {
  if(other.tag == "AI_gate")
    gates.Add(other.gameObject);
    Debug.Log(gameObject.name + " : " + gates.length);
}

AI_gate

#pragma strict

var areas = new Array();
var numberOfGates = new Array();
var touchingAreas = new Array();
var findAreas : boolean = true;

@HideInInspector
var findAreasDelay : float = 2f;//Delay before we find the areas

@HideInInspector
var phase : int = 0;

@HideInInspector
var doorOpen : boolean = true;//Is the door unlocked


function Start () {
  doorOpen = true;
  areas = GameObject.FindGameObjectsWithTag("AI_area");
  numberOfGates.length = areas.length;
  findAreas = true;
  findAreasDelay = 2f;
  phase = 0;
}

function Update () {

  if(findAreas  findAreasDelay <= 0) {
    for(var touchingArea : GameObject in touchingAreas) {
      for(var i = 0; i <= areas.length-1; i++) {
        if(areas[i] == touchingAreas) {
          numberOfGates[i] = 1;
        }
      }
    }
    for(phase = 2; phase <= areas.length-1; phase++) {
      for(i = 0; i <= areas.length-1; i++) {
        if(numberOfGates[i] == phase - 1) {
          var areaG : GameObject = areas[i];
          var currentAreaGates = areaG.GetComponent(AI_Area).gates.length;
          for(var l = 0; l <= currentAreaGates; l++) {
            var checkDoor : GameObject = areaG.GetComponent(AI_Area).gates[l];
            if(checkDoor != gameObject) {
              for(var checkArea : GameObject in checkDoor.GetComponent(AI_Gate).touchingAreas) {
                for(var j = 0; j <= areas.length-1; j++) {
                  if(areas[i] == checkArea  numberOfGates[i] == null) 
                      numberOfGates[j] = phase;
                }
              }
            }
          }
        }
      }
    }
    
  
  
  
    findAreas = false;
 }
  
  findAreasDelay--;

}

//Get the touching areas
function OnTriggerEnter(other : Collider) {
  
  if(other.tag == "AI_area")
    touchingAreas.Add(other.gameObject);

}

After a lot of work i got it down to one line. I marked the line in red below so that it’s easy to find. I still haven’t been able to fix it though.

var areas = new Array();

var numberOfGates = new Array();

var touchingAreas = new Array();

var findAreas : boolean = true;

var gates : int[ ];//For debuging

@HideInInspector

var findAreasDelay : float = 2f;//Delay before we find the areas

@HideInInspector

var phase : int = 0;

@HideInInspector

var doorOpen : boolean = true;//Is the door unlocked

function Awake () {

doorOpen = true;

areas = GameObject.FindGameObjectsWithTag(“AI_area”);

numberOfGates.length = areas.length;

findAreas = true;

findAreasDelay = 2f;

phase = 0;

}

//Get the touching areas

function OnTriggerEnter(other : Collider) {

if(other.tag == “AI_area”) {
touchingAreas.Add(other.gameObject);
Debug.Log("Added touching area: "+ other.gameObject.name + " to " + gameObject.name);
}

}

function Update () {

if(findAreas findAreasDelay <= 0) {

for(var touchingArea : GameObject in touchingAreas) {
Debug.Log(“phase start 1”);
for(var i = 0; i <= areas.length-1; i++) {
Debug.Log(“phase marker 1”);
if(areas == touchingArea) {
numberOfGates = 1;
Debug.Log(“phase end 1”);
}
}
}
for(phase = 2; phase <= areas.length-1; phase++) {
Debug.Log("phase start " + phase);
for(i = 0; i <= areas.length-1; i++) {

if(numberOfGates == phase - 1) {

var areaG : GameObject = areas*;*
var currentAreaGates = areaG.GetComponent(AI_Area).gates.length;

for(var l = 0; l <= currentAreaGates-1; l++) {
var checkDoor : GameObject = areaG.GetComponent(AI_Area).gates[l];

if(checkDoor != gameObject) {

for(var checkArea : GameObject in checkDoor.GetComponent(AI_Gate).touchingAreas) {

for(var j = 0; j <= areas.length-1; j++) {
//Debug.Log("phase marker " + phase);
if(areas == checkArea numberOfGates == null[/COLOR]) {
Debug.Log("phase marker " + phase);
numberOfGates[j] = phase;
gates = numberOfGates;
Debug.Log("phase end " + phase);
}
}
}
}
}
}
}
}
Debug.Log(numberOfGates);

/*
var outString = “”;
for(var i2 = 0; i2 < numberOfGates.length; i2++) {
outString = outString + ", " + numberOfGates[i2];
}
Debug.Log(gameObject.name + " " + outString);
*/

findAreas = false;
}

findAreasDelay-=1;

}[/SIZE]

I think i’v fixed it.

var areas = new Array();

var numberOfGates = new Array();

var touchingAreas = new Array();

var findAreas : boolean = true;

var gates : int[];//For debuging

@HideInInspector

var findAreasDelay : float = 2f;//Delay before we find the areas

 

@HideInInspector

var phase : int = 0;

 

@HideInInspector

var doorOpen : boolean = true;//Is the door unlocked

 

 

function Awake () {

  doorOpen = true;

  areas = GameObject.FindGameObjectsWithTag("AI_area");

  numberOfGates.length = areas.length;
  
  findAreas = true;

  findAreasDelay = 2f;

  phase = 0;

}

//Get the touching areas

function OnTriggerEnter(other : Collider) {


  if(other.tag == "AI_area") {
    touchingAreas.Add(other.gameObject);
    Debug.Log("Added touching area: "+ other.gameObject.name + " to " + gameObject.name);
  }
 
}

function Update () {

 

  if(findAreas  findAreasDelay <= 0) {

    for(var touchingArea : GameObject in touchingAreas) {
      Debug.Log("phase start 1");
      for(var i = 0; i <= areas.length-1; i++) {
        Debug.Log("phase marker 1");
        if(areas[i] == touchingArea) {

          numberOfGates[i] = 1;
          Debug.Log("phase end 1");
        }

      }

    }

    for(phase = 2; phase <= areas.length-1; phase++) {
      Debug.Log("phase start " + phase);
      for(i = 0; i <= areas.length-1; i++) {
        
        if(numberOfGates[i] == phase - 1) {
          
          var areaG : GameObject = areas[i];

          var currentAreaGates = areaG.GetComponent(AI_Area).gates.length;
          
          for(var l = 0; l <= currentAreaGates-1; l++) {

            var checkDoor : GameObject = areaG.GetComponent(AI_Area).gates[l];
            
            if(checkDoor != gameObject) {
              
              for(var checkArea : GameObject in checkDoor.GetComponent(AI_Gate).touchingAreas) {
                
                for(var j = 0; j <= areas.length-1; j++) {
                  Debug.Log("phase marker " + phase);
                  gates = numberOfGates;
                  if(areas[j] == checkArea  numberOfGates[j] == null) {
                      numberOfGates[j] = phase;
                      gates = numberOfGates;
                      Debug.Log("phase end " + phase);
                  }
                }

              }

            }

          }

        }

      }

    }
    Debug.Log(numberOfGates);

  
    /*
    var outString = "";
    for(var i2 = 0; i2 < numberOfGates.length; i2++) {
      outString = outString + ", " + numberOfGates[i2];
    }
    Debug.Log(gameObject.name + " " + outString);
    */
    
    findAreas = false;

 }

  

  findAreasDelay-=1;

 

}

Remember to set numberOfGates, else your numberOfGates == null check wont work as numberOfGates is null