- Home /
Question by
Martacus · Sep 23, 2014 at 11:21 AM ·
c#2dnullreferenceexception
Got a Error about reffering a object i can not fix
So i have this Basic AI for a mob to follow the player in 2D but it wont work. Can somebody help me with this? This is the Error:
NullReferenceException: Object reference not set to an instance of an object EnemyAI.AI () (at Assets/Scripts/EnemyAI.cs:24) EnemyAI.Update () (at Assets/Scripts/EnemyAI.cs:19)
Code: using UnityEngine; using System.Collections;
public class EnemyAI : MonoBehaviour
{
Transform Hero;
public float AISpeed = 5f;
public float MaxDistance = 10f;
public float Mindistance = 2f;
void Start ()
{
Hero = GameObject.FindGameObjectWithTag("player").transform;
}
void Update ()
{
transform.LookAt (Hero);
AI ();
}
void AI()
{
if(Vector3.Distance (transform.position, Hero.position) >= Mindistance)
{
transform.position += transform.forward*AISpeed*Time.deltaTime;
if(Vector3.Distance (transform.position, Hero.position) >= MaxDistance)
{
}
}
}
}
Comment
Your answer
![](https://koobas.hobune.stream/wayback/20220613163615im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Referencing variables from another script 2 Answers
Object Reference not set to instance of an object - error in 2d rope code 0 Answers
if it's you, how will you solve it ? 1 Answer