Search Unity

Zombie AI not working?!

Discussion in 'Scripting' started by epichi123, Sep 1, 2014.

  1. epichi123

    epichi123

    Joined:
    Feb 15, 2014
    Posts:
    16
    My zombie isn't working and not quite sure what the problem is. I got this script off of a website and it was working before but now it isn't. I know this should be posted on unity answers instead, but it was rejected twice so I'm looking for here.

    Here is the error: NullReferenceException: Object reference not set to an instance of an object Zombie.FixedUpdate () (at Assets/scripts/Scene5 (level 1)/Zombie.js:18)

    Here is the script:

    Code (JavaScript):
    1.     #pragma strict
    2.     varVisionDistance:float=200;
    3.     varMovementSpeed:float=2;
    4.     varHealth:int=3;
    5.     functionFixedUpdate(){
    6.     // Get the Player object
    7.     var player :GameObject=GameObject.Find("Player");
    8.     var characterController :CharacterController=GetComponent(CharacterController);
    9.     // Get the position of the Zombie's eyes
    10.     var eyePosition :Vector3= transform.position;
    11.     eyePosition.y += characterController.height;
    12.     // Get the difference between the player and the Zombie positions
    13.     // This creates a direction vector pointing in the direction of the Player.
    14.     var lookDirection = player.transform.position - eyePosition;
    15.     lookDirection = lookDirection.normalized;
    16.     // Only look for the player or objects that are part of the scenery (terrain, buildings, etc.)
    17.     var layerMask :int=1<<LayerMask.NameToLayer("Player")|1<<LayerMask.NameToLayer("Default");
    18.     // The direction the Zombie will move, defaults to standing still
    19.     var movementDirection :Vector3=Vector3.zero;
    20.     // hitInfo will contain information about what the Zombie can see.
    21.     var hitInfo :RaycastHit;
    22.     if(Physics.Raycast(eyePosition, lookDirection, hitInfo,VisionDistance, layerMask)){
    23.     // If the Zombie can see the Player move toward them.
    24.     if(hitInfo.collider.gameObject == player){
    25.     movementDirection = lookDirection;
    26.     movementDirection.y =0;
    27.     movementDirection = movementDirection.normalized;
    28.     }
    29.     }
    30.     // Face and move in the chosen direction
    31.     if(movementDirection !=Vector3.zero){
    32.     transform.rotation =Quaternion.LookRotation(movementDirection,Vector3.up);
    33.     }
    34.     characterController.SimpleMove(movementDirection *MovementSpeed);
    35.     }
    Any help is appreciated.
     
    Last edited: Sep 3, 2014
  2. smitchell

    smitchell

    Joined:
    Mar 12, 2012
    Posts:
    702
    First of all use code tags. Code isn't readable without them.

    As for your error, it's saying there is a null reference. So in your script you're trying to get access to something that doesn't exist
     
  3. epichi123

    epichi123

    Joined:
    Feb 15, 2014
    Posts:
    16
    Thanks for the help but I still can't find the problem. :(

    If it helps, this is the line of the error (line 14):
    var lookDirection = player.transform.position - eyePosition;
     
  4. smitchell

    smitchell

    Joined:
    Mar 12, 2012
    Posts:
    702
  5. epichi123

    epichi123

    Joined:
    Feb 15, 2014
    Posts:
    16
    Thanks for the help with code tags. :)