0

I have a script with animations called Widget_Animation and an error massage saying:

NullReferenceException: Object reference not set to an instance of an object****Widget_Animation.Start () (at Assets/Scripts/Javascripts/Widget_Animation.js:14)

Here is the script.

//Widget_Animation: Animation State Manager for Widget.
//controls layers, blends, and play cues for all imported animations

private var nextPlayIdle = 0.0;
var waitTime = 8.0;

var playerController: Widget_Controller; 
playerController = GetComponent(Widget_Controller) ;

//Initialize and set up all imported animations with proper layers--------
function Start(){

//set up layers - high numbers receive priority when blending
   ((GetComponent.<Animation>() as Animation)["Widget_Idle"] as AnimationClip).layer = -1;
   ((GetComponent.<Animation>() as Animation)["Idle"] as AnimationClip).layer = 0;

//we want to make sure that the rolls are synced together
     ((GetComponent.<Animation>() as Animation)["SlowRoll"] as AnimationClip).layer = 1;
     ((GetComponent.<Animation>() as Animation)["FastRoll"] as AnimationClip).layer = 1;
     ((GetComponent.<Animation>() as Animation)["Duck"] as AnimationClip).layer = 1;
     ((GetComponent.<Animation>() as Animation)as AnimationClip).SyncLayer(1);

     ((GetComponent.<Animation>() as Animation)["Taser"] as AnimationClip).layer = 3;
     ((GetComponent.<Animation>() as Animation)["Jump"] as AnimationClip).layer = 5;

//these should take priority over all others
     ((GetComponent.<Animation>() as Animation)["FallDown"] as AnimationClip).layer = 7;
     ((GetComponent.<Animation>() as Animation)["GotHit"] as AnimationClip).layer = 8;
     ((GetComponent.<Animation>() as Animation)["Die"] as AnimationClip).layer = 10;

     ((GetComponent.<Animation>() as Animation)["Widget_Idle"] as AnimationClip).wrapMode = WrapMode.PingPong;
     ((GetComponent.<Animation>() as Animation)["Duck"] as AnimationClip).wrapMode = WrapMode.Loop;
     ((GetComponent.<Animation>() as Animation)["Jump"] as AnimationClip).wrapMode = WrapMode.ClampForever;
     ((GetComponent.<Animation>() as Animation)["FallDown"] as AnimationClip).wrapMode = WrapMode.ClampForever;

//Make sure nothing is playing by accident, then start with a default idle.
    GetComponent.<Animation>().Stop();
    GetComponent.<Animation>().Play("Idle");

}

//Check for which animation to play------------------------------------
function Update(){

    //on the ground animations
    if(playerController.IsGrounded()){

        GetComponent.<Animation>().Blend("FallDown", 0, 0.2);
        GetComponent.<Animation>().Blend("Jump", 0, 0.2);

        //if boosting
        if (playerController.IsBoosting())
        {
            GetComponent.<Animation>().CrossFade("FastRoll", 0.5);
            nextPlayIdle = Time.time + waitTime;
        }

        else if(playerController.IsDucking()){

            GetComponent.<Animation>().CrossFade("Duck", 0.2);
            nextPlayIdle = Time.time + waitTime;
        }

        // Fade in normal roll
        else if (playerController.IsMoving())
        {
            GetComponent.<Animation>().CrossFade("SlowRoll", 0.5);
            nextPlayIdle = Time.time + waitTime;
        }
        // Fade out walk and run
        else
        {
            GetComponent.<Animation>().Blend("FastRoll", 0.0, 0.3);
            GetComponent.<Animation>().Blend("SlowRoll", 0.0, 0.3);
            GetComponent.<Animation>().Blend("Duck", 0.0, 0.3);
            if(Time.time > nextPlayIdle){
                nextPlayIdle= Time.time + waitTime;
                PlayIdle();
            }
            else
                GetComponent.<Animation>().CrossFade("Widget_Idle", 0.2);
        }
    }
    //in air animations
    else{
        if(Input.GetButtonDown("Jump")){

            GetComponent.<Animation>().CrossFade("Jump");
        }

        if(!playerController.IsGrounded()){

            GetComponent.<Animation>().CrossFade("FallDown", 0.5);    
        }
    }

//test for idle
    if(Input.anyKey){

        nextPlayIdle = Time.time + waitTime;
    }
}

//Other functions--------------------------------------------
function PlayTaser(){

    GetComponent.<Animation>().CrossFade("Taser", 0.2);
}

function PlayIdle(){

    GetComponent.<Animation>().CrossFade("Idle", 0.2);
}

function GetHit(){

    GetComponent.<Animation>().CrossFade("GotHit", 0.2);
}

function PlayDie(){

    GetComponent.<Animation>().CrossFade("Die", 0.2);
}

@script AddComponentMenu("Player/Widget'AnimationManager")

The error is in this line

((GetComponent.<Animation>() as Animation)["Widget_Idle"] as AnimationClip).layer = -1;

and probably in every other animation line in the code. Any ideas how to fix it becuase i have been stuck on it for weeks. Thanks in advance

StevenWhite
  • 5,474
  • 2
  • 16
  • 43
  • Are you sure you didn't mean to actually write `GetComponent.()` in `((GetComponent.() as Animation)["Widget_Idle"] as AnimationClip).layer`? – Serlite Feb 23 '17 at 21:48
  • 1
    @XanderLuciano Eeer...I'm pretty sure that documentation is indicating that the public field `GameObject.animation` was removed in 5.5.1, and `GetComponent()` should now be used to access the animation component. – Serlite Feb 23 '17 at 22:08
  • Ugh my bad, I'm just gunna delete that comment so I don't confuse someone since I can't edit it. Thank you. – Xander Luciano Feb 23 '17 at 22:10
  • Can you post a picture of your component? – Juan Bayona Beriso Feb 23 '17 at 22:37
  • Possible duplicate of [What is a NullReferenceException, and how do I fix it?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Hellium Feb 23 '17 at 22:50

0 Answers0