Hello Everyone
I’m getting an annoying error in my scrips and I’m wondering what this means. Can someone please help me out.
Full Error:
Object reference not set to an instance of an object
AnimatorSetup.Setup (Single speed, Single angle) (at Assets/Scripts/AnimatorSetup.cs:29)
EnemyAnimation.NavAnimSetup () (at Assets/Scripts/EnemyAnimation.cs:61)
EnemyAnimation.Update () (at Assets/Scripts/EnemyAnimation.cs:35)
AnimatorSetup
using UnityEngine;
using System.Collections;
public class AnimatorSetup : MonoBehaviour {
public float speedDampTime = 0.1f;
public float angularSpeedDampTime = 0.7f;
public float angleResponseTime = 0.6f;
private Animator anim;
private HashID hash;
public AnimatorSetup(Animator animator, HashIDhashIDs)
{
anim = animator;
hash = hashIDs;
}
public void Setup(float speed, float angle)
{
float angularSpeed = angle / angleResponseTime;
anim.SetFloat(hash.speedFloat, speed, speedDampTime, Time.deltaTime);
anim.SetFloat(hash.angularSpeedFloat, angularSpeed, angularSpeedDampTime, Time.deltaTime);
}
}
EnemyAnimation Script
using UnityEngine;
using System.Collections;
public class EnemyAnimation : MonoBehaviour {
public float deadZone = 5f;
public float speed;
public float angle;
private Transform player;
private EnemySightenemySight;
private NavMeshAgentnav;
private Animator anim;
private HashID hash;
private AnimatorSetupanimSetup;
void Awake()
{
player = GameObject.FindGameObjectWithTag(“Player”).transform;
enemySight = GetComponent();
nav = GetComponent ();
anim = GetComponent ();
hash = GetComponent ();
nav.updateRotation = false;
animSetup = new AnimatorSetup(anim, hash);
anim.SetLayerWeight (1, 1f);
anim.SetLayerWeight (2, 1f);
deadZone *= Mathf.Deg2Rad;
}
void Update()
{
NavAnimSetup (); //error
}
void OnAnimatorMove()
{
nav.velocity = anim.deltaPosition / Time.deltaTime;
transform.rotation = anim.rootRotation;
}
void NavAnimSetup()
{
if (enemySight.playerinSight) {
speed = 0f;
angle = FindAngle (transform.forward, player.position - transform.position, transform.up);
} else {
speed = Vector3.Project(nav.desiredVelocity, transform.forward).magnitude;
angle = FindAngle (transform.forward, nav.desiredVelocity, transform.up);
if(Mathf.Abs(angle) < deadZone)
{
transform.LookAt(transform.position + nav.desiredVelocity);
angle = 0f;
}
}
animSetup.Setup (speed,angle); //error
}
float FindAngle(Vector3 fromVector, Vector3 toVector, Vector3 UpVector)
{
if (toVector == Vector3.zero)
return 0f;
float angle = Vector3.Angle (fromVector, toVector);
Vector3 normal = Vector3.Cross (fromVector, toVector);
angle *= Mathf.Sign(Vector3.Dot(normal, UpVector));
angle *= Mathf.Deg2Rad;
return angle;
}
}
Thanks
Minpin