- Home /
"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;
}
}
Can you post the error which comes in the console? That way we'll know exactly what is causing the problem. Just make sure that all of your public variables are assigned in the inspector and the gameObjects which have the EnemyAI script both have a Nav$$anonymous$$eshAgent and a PlanetLife component.
Evil Tak this is the error: "NullReferenceException: Object reference not set to an instance of an object EnemyAI.StateHitting () (at Assets/Scripts/EnemyAI.cs:77)EnemyAI.Update () (at Assets/Scripts/EnemyAI.cs:49)" And all variables are assigned... I don't know why it's happening...
Answer by static_cast · Dec 07, 2014 at 10:15 PM
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! ;)
Answer by Baste · Dec 07, 2014 at 10:20 PM
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.
He alread has an instance of the planet in the target transform, so FindObjectOfType would be costly and he would have to figure out which planet to target with more planets in the scene.
Thanks so much you both, I understood my error, and fixed it! :)
Your answer
Follow this Question
Related Questions
NullReferenceException:"Object reference not set to an instance object" 1 Answer
Script decides not to run anymore 1 Answer
Object reference not set to an instance of an object 0 Answers
NullReferenceException: Object reference not set to an instance of an object ? 1 Answer
i don't understand "NullReferenceException: Object reference not set to an instance of an objec" 0 Answers