Nullreferenceexception: Object Reference not set to an instance of an Object

Asked

Viewed 212 times

0

I’m making this mistake:

Nullreferenceexception: Object Reference not set to an instance of an Object Enemymovement.Awake () (at Assets/Scripts/Enemy/Enemymovement.Cs:13)

Code:

using UnityEngine;
using System.Collections;

public class EnemyMovement : MonoBehaviour
{
    Transform player;

    NavMeshAgent nav;


    void Awake ()
    {
        player = GameObject.FindGameObjectWithTag ("Player").transform;

        nav = GetComponent <NavMeshAgent> ();
    }


    void Update ()
    {
        //if(enemyHealth.currentHealth > 0 && playerHealth.currentHealth > 0)
        //{
            nav.SetDestination (player.position);
        //}
        //else
        //{
        //    nav.enabled = false;
        //}
    }
}
  • Can you prove that the tag is exactly Player, and that it is already on the scene to be accessed by the method? Because there is nothing wrong with your code.

  • very good. thank you.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.