Enemy AI Error
Hey guys. I'm having a problem with my Enemy AI moving towards my player. It keeps saying "Object reference is not set to an instance of the Object". Think anyone can help me fix it? My code is this;
public class AI1 : MonoBehaviour {
private Vector3 Player;
private Vector2 Playerdirection;
private float Xdif;
private float Ydif;
private float speed;
void start () {
speed = 10;
}
void Update () {
Player = GameObject.FindWithTag("Player").transform.position;
Xdif = Player.x - transform.position.x;
Ydif = Player.y - transform.position.y;
Playerdirection = new Vector2 (Xdif, Ydif);
GetComponent<Rigidbody2D>().AddForce(Playerdirection.normalized * speed);
}
}
Answer by TBruce · Apr 14, 2016 at 01:47 AM
I deleted my answer because I saw right after posting it that @jgodfrey had basically posted the same thing my I only corrected the script. but @jgodfrey is correct that you really should be initializing those variable in Start() or Awake() like so
private Vector3 player;
private Vector2 playerdirection;
private float xDif;
private float yDif;
private float speed;
private Rigidbody2D rigidbody = null;
void start ()
{
speed = 10;
// only get the player once instead of every frame
player = GameObject.FindWithTag("Player");
// only get the rigidbody once instead of every frame
rigidbody = GetComponent<Rigidbody2D>();
System.String error = "";
if (player == null)
{
error = "GameObject.FindWithTag(Player) == null, ";
}
if (rigidbody == null)
{
error += "GetComponent<Rigidbody2D>() == null";
}
// only log the error once instead of every frame
if (error != "")
{
Debug.Log(error);
}
}
void Update ()
{
if ((player != null) && (rigidbody != null))
{
Vector3 position = player.transform.position;
xDif = player.x - transform.position.x;
yDif = player.y - transform.position.y;
playerdirection = new Vector2 (xDif, yDif);
rigidbody.AddForce(playerdirection.normalized * speed);
}
}
Answer by jgodfrey · Apr 13, 2016 at 01:05 AM
Is the error here...
Player = GameObject.FindWithTag(...)
or here...
GetComponent<Rigidbody2D>()...
If the error is in the first one, do you have an in-game object tagged as "Player" (with the exact same spelling and character case)? If it's in the second one, does the object the above script is attached to have a Rigidbody2D component?
Regardless of the error, both of those lines are not very efficient and should not be done in Update() (you're looking that information up in every frame - and they won't change from frame to frame)...
You really should look those things up in Start() or Awake() once and cache a reference to them. Then, use that cached reference in Update - it'll be much more efficient.
Answer by TheMightyFlash · Apr 14, 2016 at 01:24 AM
Alright, so my problem was solved by the user @Mavina (Thank you very much!!!). Unfortunately, the answer was deleted, so I will put a copy of it into this comment! Thank you again @Mavina!! (Here's the fully fixed script!)
public class AI1 : MonoBehaviour {
private Vector3 Player;
private Vector2 Playerdirection;
private float Xdif;
private float Ydif;
public float speed;
void start()
{
speed = 10;
}
void Update()
{
if ((GameObject.FindWithTag("Player")) && (GetComponent<Rigidbody2D>()))
{
Player = GameObject.FindWithTag("Player").transform.position;
Xdif = Player.x - transform.position.x;
Ydif = Player.y - transform.position.y;
Playerdirection = new Vector2(Xdif, Ydif);
GetComponent<Rigidbody2D>().AddForce(Playerdirection.normalized * speed);
}
else
{
System.String error = "";
if (GameObject.FindWithTag("Player") == null)
{
error = "GameObject.FindWithTag(Player) == null, ";
}
if (GetComponent<Rigidbody2D>() == null)
{
error += "GetComponent<Rigidbody2D>() == null";
}
Debug.Log(error);
}
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220612074725im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Enemy AI Script 1 Answer
Trouble making jumping spider enemies 0 Answers
Attaching objects to scripts without dragging and dropping 0 Answers
2D Enemy Ai 0 Answers
Trying to make an enemyAI and have a few issues could I get some help? 0 Answers