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 working?!

Discussion in 'Scripting' started by epichi123, Sep 20, 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.         var VisionDistance:float=200;
    3.         var MovementSpeed:float=2;
    4.         var Health:int=3;
    5.         function FixedUpdate(){
    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.         }
    36.  
    Any help is appreciated.
     
    Last edited: Sep 21, 2014
  2. Avo

    Avo

    Joined:
    Dec 4, 2010
    Posts:
    236
    I'm not sure which line it's highlighting for you in the editor, line 18 is a comment for the code you posted. The gist of the error is that it's expecting an object to be input somewhere but it's finding nothing. Did you maybe forget to assign a variable in the inspector?
     
  3. elmar1028

    elmar1028

    Joined:
    Nov 21, 2013
    Posts:
    2,355
    functionFixed Update() is glued together. Line 5
     
  4. epichi123

    epichi123

    Joined:
    Feb 15, 2014
    Posts:
    16
    I don't know why it did that when I pasted it onto the thread. Thanks for telling me.
     
  5. epichi123

    epichi123

    Joined:
    Feb 15, 2014
    Posts:
    16
    I checked, and I assigned all the variables.
     
  6. shkar-noori

    shkar-noori

    Joined:
    Jun 10, 2013
    Posts:
    833
    your gameobject's name must be 'player', you have done something terribly wrong using GameObject.Find in FixedUpdate, you should use it in Start and cache the reference
     
  7. epichi123

    epichi123

    Joined:
    Feb 15, 2014
    Posts:
    16
    I tried it and I got some errors:

    ERRORS:
    Assets/scripts/Scene5 (level 1)/Zombie.js(14,22): BCE0005: Unknown identifier: 'characterController'.
    Assets/scripts/Scene5 (level 1)/Zombie.js(18,25): BCE0005: Unknown identifier: 'player'.
    Assets/scripts/Scene5 (level 1)/Zombie.js(31,44): BCE0005: Unknown identifier: 'player'.
    Assets/scripts/Scene5 (level 1)/Zombie.js(42,5): BCE0005: Unknown identifier: 'characterController'.

    SCRIPT:
    Code (JavaScript):
    1. var VisionDistance : float = 200;
    2. var MovementSpeed : float = 2;
    3. var Health : int = 2;
    4.  
    5. function Start () {
    6.     var player : GameObject = GameObject.Find("Player");
    7.     var characterController : CharacterController = GetComponent(CharacterController);
    8. }
    9.  
    10. function FixedUpdate () {
    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. }
     
  8. BmxGrilled

    BmxGrilled

    Joined:
    Jan 27, 2014
    Posts:
    238
    You need to define the player var as global, so you assign it in the start function just as you are, but instead of the var being there. you add a line with the other vars at the top of your script as follows:
    var player : GameObject;

    Hope this helps! :)
     
  9. elmar1028

    elmar1028

    Joined:
    Nov 21, 2013
    Posts:
    2,355
    Then try to manually assign a player gameobject from inspector (just remove GameObject.find). I had this before and this fixed my issue.
     
  10. epichi123

    epichi123

    Joined:
    Feb 15, 2014
    Posts:
    16
    Thanks, I'll try that today.
     
  11. epichi123

    epichi123

    Joined:
    Feb 15, 2014
    Posts:
    16
    For some reason, when it looks for the player, it can't find it. And if I try to drag it in in the inspecter, it won't let me.

    Error:
    NullReferenceException: Object reference not set to an instance of an object
    Zombie.FixedUpdate () (at Assets/scripts/Scene5 (level 1)/Zombie.js:20)
     
    Last edited: Sep 22, 2014