This question was
closed Jan 28, 2018 at 11:09 PM by
alphanike97 for the following reason:
The question is answered, right answer was accepted
Question by
alphanike97 · Jan 28, 2018 at 10:56 PM ·
errorvector3aidistance
Error with vector3.distance
Hi, could you tell me why the Vector3.Distance function gives me an error of "Object reference not set to an object"
using UnityEngine;
using System.Collections;
using UnityEngine.AI;
public class IAbasica : MonoBehaviour {
NavMeshAgent agent;
GameObject target;
Animator anim;
public float Health;
public float currentDistance;
public UnityStandardAssets.Characters.FirstPerson.RigidbodyFirstPersonController rb;
public int DamagePlayerF;
private void FixedUpdate() {
currentDistance = Vector3.Distance(transform.position, target.transform.position);
}
void Update () {
anim = GetComponent<Animator>();
agent = GetComponent<NavMeshAgent>();
target = GameObject.FindGameObjectWithTag("Player");
agent.SetDestination(target.transform.position);
if (currentDistance <= 3f) {
StartCoroutine(DamagePlayer());
}
}
IEnumerator DamagePlayer() {
rb.TotalHealth = rb.CurrentHealth - DamagePlayerF;
yield return new WaitForSeconds(3f);
}
public void TakeDamage(float amount){
Health -= amount;
if(Health <= 0f)
Die();
}
void Die (){
Destroy (gameObject);
}
}
Comment
Probably "target" is null.
if (target != null)
{
Debug.Log("Yay found target!");
currentDistance = Vector3.Distance(transform.position, target.transform.position);
}
else
{
Debug.Log("Target was null!");
}
But this script has more errors...
Follow this Question
Related Questions
Unexpected symbol 'return' in class, struct, or interface member declaration 3 Answers
NullReferenceException: Object reference not set to an instance of an object 1 Answer
Unity Vector3.Distance calculating wrong 1 Answer
how to check if an object is betwen enemy and player 1 Answer
Move GameObject to Point A to B 1 Answer