Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Zombie AI not pointing towards the player?

Discussion in 'Scripting' started by epichi123, Sep 30, 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.

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

    Barachiel

    Joined:
    Nov 25, 2012
    Posts:
    147
    It's not finding the player object you're looking for on line 9, so when it tries to use player.transform.position, it returns null.
    Make sure you have a character in the scene named Player, or get a reference to the player character some other way and use that. =)
     
  3. epichi123

    epichi123

    Joined:
    Feb 15, 2014
    Posts:
    16
    How else can I get a referance to the player?
     
  4. Barachiel

    Barachiel

    Joined:
    Nov 25, 2012
    Posts:
    147
    Well, you could change this line:
    Code (JavaScript):
    1. var player : GameObject = GameObject.Find("Player");
    Just replace the name Player with whatever name your player character has.

    Alternatively, you could do this:
    Code (JavaScript):
    1. var player : GameObject;
    And assign the player object in the inspector.
     
  5. epichi123

    epichi123

    Joined:
    Feb 15, 2014
    Posts:
    16
    The player object is called Player. Oh and, the zombie is a prefab to be spawned.