"Object reference not set to an instance of an object"

Hello everybody.

I’m making a 2D game that likes a tower defence. I’m writing the enemy’s AI scripts that will go to the planet and start to attack it. But I can’t make the enemies take out the planet’s life, and I don’t know why…

I’m new in this, can you help me?

Script called “EnemyAI” it’s the enemies’ AI, the “PlanetLife” it’s the planet’s life.

Enemies AI’s code:

using UnityEngine;
using System.Collections;
using DG.Tweening;

public class EnemyAI : MonoBehaviour {

    public Transform target;
    NavMeshAgent spaceship;
    public Vector3 distTarget;

    private PlanetLife life;

    public enum States
    {
        Invading,
        Hitting,
        Attacking
    }

    public States state = States.Invading;

    void Awake()
    {
        life = GetComponent<PlanetLife> ();
    }

    void Start ()
    {
        spaceship = GetComponent<NavMeshAgent> ();
    }


    void Update () {

        switch(state)
        {

        case States.Invading:
            StateInvading();

            break;

        case States.Attacking:
            StateAttacking();
             
            break;

        case States.Hitting:
            StateHitting();

            break;

        default :
            Debug.LogError("BUG: Cannot stay without state.");
         
            break;
         
        }

    }

    private void StateInvading()
    {
        distTarget = target.position - transform.position;
        spaceship.SetDestination (target.position);

        if (distTarget.magnitude < 15)
        {
            state = States.Hitting;
        }
    }

    private void StateHitting()
    {
 
        spaceship.Stop ();
        life.LIFE -= 10;
        Debug.Log (life.LIFE + " remaining lives.");

    }
 

    private void StateAttacking()
    {

    }
}

Planet’s life code

using UnityEngine;
using System.Collections;

public class PlanetLife : MonoBehaviour
{
    public float LIFE;
 
    public void Start ()
    {
        LIFE = 1000;
    }
}

Your error is telling you that this line (77) in StateHitting() is causing the error:

life.LIFE -= 10;

That line causing a nullReference means that life is null - not assigned to anything.

You’re fetching the life variable in Awake like this:

life = GetComponent<PlanetLife> ();

If life is null after that, the GetComponent call found no PlanetLife script on the same object as the EnemyAI script is on. Which makes sense - it doesn’t sound like they belong in the same place.

If you’re only going to have a single PlanetLife script in your scene, replace GetComponent with FindObjectOfType, and you’ll be golden. Otherwise, you’ll need to rethink what you’re doing - but the bottom line is that GetComponent doesn’t find any component with that class, only components attached to the same object in the scene.

There error is probably here: life = GetComponent();

I think what you mean to say is life = target.gameObject.GetComponent();

The reason is that you are getting the current gameObjects component, rather than the target’s.

Hope this helps! :wink: