Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

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

Discussion in 'Scripting' started by Minpin, Jul 1, 2015.

  1. Minpin

    Minpin

    Joined:
    Apr 17, 2014
    Posts:
    3
    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<EnemySight>();

    nav = GetComponent<NavMeshAgent> ();

    anim = GetComponent<Animator> ();

    hash = GetComponent<HashID> ();

    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
     
  2. shawnrevels

    shawnrevels

    Joined:
    Aug 13, 2014
    Posts:
    86
    Im assuming you dont have animation compnent set to object in reference?
     
  3. shawnrevels

    shawnrevels

    Joined:
    Aug 13, 2014
    Posts:
    86
    Animator Component i meant
     
  4. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    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 :p
     
  5. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    Code (csharp):
    1.  
    2. public class AnimatorSetup : MonoBehaviour {...}
    3. // animatorSetup inherit from Monobehaviour so it's a component not a c# class
    4.  
    5.  
    6. animSetup = new AnimatorSetup(anim, hash);
    7.  
    this is wrong, you cannot add a component with "new"

    I think you were looking for
    Code (csharp):
    1.  
    2. GetComponent<AnimatorSetup> ().AnimatorSetup(anim, hash);
    3.  
    assuming AnimatorSetup is already a component on this gameObject, if not you're looking for AddComponent<T>()
     
  6. Minpin

    Minpin

    Joined:
    Apr 17, 2014
    Posts:
    3
    So is this what is supposed to look like?
    Code (csharp):
    1.  
    2. animSetup = GetComponent<AnimatorSetup> ().AnimatorSetup(anim, hash);
    3.  
    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
     
  7. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    not quite... if you want to get a reference to the AnimatorSetup component and store it in animSetup you want something like

    Code (csharp):
    1.  
    2. animSetup = GetComponent<AnimatorSetup>();
    3.  
    and then you can use the reference for the function call
    Code (csharp):
    1.  
    2. animSetup.AnimatorSetup(anim, hash);
    3.  
    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.