- Home /
EnemyAI C# Error
Hi! I've just got an Small error and i don't know why :/ I Get A NullReferenceException error in Line 33 And i don't know :/
Here's the Script:
using UnityEngine; using System.Collections;
public class EnemyAI : MonoBehaviour { public int minRange; public bool follow; public float moveSpeed; public float rotationSpeed; private Transform myTransform; private Transform player;
void Start()
{
myTransform = transform;
player = GameObject.FindGameObjectWithTag("Player").transform;
}
void Update ()
{
if(player != null && Vector3.Distance(player.position, myTransform.position) < minRange)
{
follow=true;
}
if(follow && player != null)
{
myTransform.rotation = Quaternion.Slerp(myTransform.rotation, Quaternion.LookRotation(player.position - myTransform.position), rotationSpeed * Time.deltaTime);
myTransform.position += myTransform.forward * moveSpeed * Time.deltaTime;
}
if(player == null) { GameObject.FindGameObjectWithTag("Player").transform; } } }
Any Ideas to fix it :)?
This is the Problem:
If(player == null){ GameObject.FindGameObjectWithTag("Player").transform; }
Answer by justinl · Oct 08, 2012 at 04:30 PM
Is there an object in your scene with the tag "Player" on it? (tag, not name)
I already fixed it :) But thanks for trying to help - Yohan
Your answer
Follow this Question
Related Questions
Why doesn't this AI script work? c# 1 Answer
enemy detect player then attack - c# 1 Answer
stupid errors i can't figure out 1 Answer
Argument out of range. 1 Answer
AI script works, when we add a blocked function it stops working HELP 0 Answers