Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

object reference not set to an instance of an object

Discussion in 'Scripting' started by Zammystuff, Apr 7, 2020.

  1. Zammystuff

    Zammystuff

    Joined:
    Jun 30, 2015
    Posts:
    9
    Can anyone help me figure out why this is happening? ive been beating my head against my desk for hours now.







    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.AI;
    5. using RPG.Combat;
    6.  
    7. namespace RPG.Control
    8. {
    9. public class AiController : MonoBehaviour
    10.     {
    11.         [SerializeField] float chaseDistance = 5.0f;
    12.         [SerializeField] float distanceFromPlayer;
    13.         Transform target;
    14.         [SerializeField] float moveSpeed;
    15.         NavMeshAgent navMeshAgent;
    16.         [SerializeField] float bufferDistance = 2.0f;
    17.  
    18.         Fighter fighter;
    19.         GameObject player;        private void Start()
    20.         {
    21.             fighter = GetComponent<Fighter>();
    22.             GameObject player = GameObject.FindWithTag("Player");
    23.         }
    24.  
    25.         private void Update()
    26.         {
    27.             if(!InAttackRangeOfPlayer() || !fighter.CanAttack(player)) return;
    28.            
    29.           if(InAttackRangeOfPlayer() && fighter.CanAttack(player))
    30.           {
    31.               fighter.Attack(player);
    32.               float distanceFromPlayer = Vector3.Distance(player.transform.position, transform.position);
    33.           }
    34.  
    35.    
    36.       }
    37.  
    38.       private bool InAttackRangeOfPlayer()
    39.       {
    40.           float distanceFromPlayer = Vector3.Distance(player.transform.position, transform.position);
    41.           return distanceFromPlayer < chaseDistance;
    42.       }
    43.          
    44.  
    45.  
    46.  
    47.         }
    48.        
    49.  
    50.     }
     
  2. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,748
    Copy and paste the error here, please. It has more information with it (specifically the line number).
     
  3. Zammystuff

    Zammystuff

    Joined:
    Jun 30, 2015
    Posts:
    9
    NullReferenceException: Object reference not set to an instance of an object
    RPG.Control.AiController.Update () (at Assets/Scripts/AiController.cs:29)

    my bad, forgot this.
     
  4. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,748
    OK, so your "fighter" variable is clearly null. You set that with .GetComponent, so is there a Fighter script attached to the same object as this script?
     
  5. Zammystuff

    Zammystuff

    Joined:
    Jun 30, 2015
    Posts:
    9
    No there is not. im drawing out all my dependencies at the moment to see where this link might be get messed up.

    Im a fairly new programmer, why would my fighter value return null?
     
  6. Zammystuff

    Zammystuff

    Joined:
    Jun 30, 2015
    Posts:
    9
    Holy S*** im stupid. Obviously it returned Null because theres nothing for it to reference on the game object. id forgotten to add the required scripts in when i remade my prefabs. I need to goto sleep. Thanks bro. youre a champ.