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. Dismiss Notice

UnassignedReferenceException on an assigned variable

Discussion in 'Scripting' started by poudigne, Jun 26, 2014.

  1. poudigne

    poudigne

    Joined:
    Jun 24, 2014
    Posts:
    7
    Hi, i'm very new to Unity (using 4.5) and i'm trying to learn unity 3D. I have this small "game" where I just want to swing an axe to kill spiders. I have created my animation named "AxeSwing" and a small script to trigger the animation when I click my left button. The probleme is that I have an error saying that my variable "attackAnimationClip" has nothing assigned to it. As you can see on the screen shot it is assigned and when I left click the animation trigger and come back to my IdleAxe animation.

    Here the script:

    Code (CSharp):
    1.  
    2.  
    3. [RequireComponent(typeof(Animation))]
    4. public class AttackAnimation : MonoBehaviour {
    5.     public AnimationClip attackAnimationClip;
    6.       // Update is called once per frame
    7.     void Update () {
    8.         if (Input.GetMouseButton(0))
    9.         {
    10.             animation.Play(attackAnimationClip.name);
    11.         }
    12.         else if (IsAttackFinished)
    13.             animation.CrossFade(animation.clip.name);
    14.     }
    15.  
    16.     private bool IsAttackFinished
    17.     {
    18.         get { return animation[attackAnimationClip.name].time > animation[attackAnimationClip.name].length;  }
    19.     }
    20. }
    21.  

    upload_2014-6-25_19-39-26.png

    Where did I go wrong ?
     
  2. katsuragi545

    katsuragi545

    Joined:
    Jun 25, 2014
    Posts:
    38
    Does animation.Play("AxeSwing"); work?
     
    poudigne likes this.
  3. poudigne

    poudigne

    Joined:
    Jun 24, 2014
    Posts:
    7
    No but it definitly pointed me in a good direction and was able to debug it. Thanks a lot !!
     
  4. Troas

    Troas

    Joined:
    Jan 26, 2013
    Posts:
    157
    What was the solution? Interested in this fix.