What does is mean if Object reference not set to an instance of an object

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

Im assuming you dont have animation compnent set to object in reference?

Animator Component i meant

please use [ code] [/ code] tags when pasting code into the forums, really helps with the readability and also provides line numbers (so we know which line the error is referring to…), there is a sticky about them at the top of the scripting forum.

@shawnrevels there is an edit button :stuck_out_tongue:

public class AnimatorSetup : MonoBehaviour {...} 
// animatorSetup inherit from Monobehaviour so it's a component not a c# class


animSetup = new AnimatorSetup(anim, hash);

this is wrong, you cannot add a component with “new”

I think you were looking for

GetComponent<AnimatorSetup> ().AnimatorSetup(anim, hash);

assuming AnimatorSetup is already a component on this gameObject, if not you’re looking for AddComponent()

So is this what is supposed to look like?

animSetup = GetComponent<AnimatorSetup> ().AnimatorSetup(anim, hash);

if so when I put in . it doesnt come up with AnimatorSetup. Hope i did the code thing right. Thanks for replying so quickly your awesome.
thanks
Minpin

not quite… if you want to get a reference to the AnimatorSetup component and store it in animSetup you want something like

animSetup = GetComponent<AnimatorSetup>();

and then you can use the reference for the function call

animSetup.AnimatorSetup(anim, hash);

that function doesn’t return anything (it’s a void function) so the code you tried will not have anything returned to set in reference. Close though :). I’d missed you use animSetup in your update function and not just in the one line in the Awake(), so I had rolled the two lines into a single lookup and execute the function.