I get the error message: NullReferenceException: Object reference not set to an instance of an object. How do I fix it?

91 Views Asked by At
    //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")

This is my code, you don't need to look thorugh the whole thing if you don't want to. The key line where the error occurs is this one, also known from the error message as line 14.

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

So can anybody help please because I am stuck on this for a week now doing nothing but searching the internet and mostly this site in particular and found nothing. The script above is part of a Unity3D project and I am kinda new to it, so please explain in detail what the problem is and how can I fix it. I have read many posts in this site and most of the problems were resolved, hopefuuly the same happens here. Love you guys! You are the best! I can always count on help via this site and very friendly comunity.

1

There are 1 best solutions below

1
On

Break down the line into several small steps to narrow down the reason, i.e.:

var c = GetComponent.<Animation>();
Debug.Log("c = " + c, c);

Use the debugger to step through and inspect the variables.

Check every line in the console, click on it to see if the right game object is highlighted in editor (provided that you have specified it as second parameter).

Check the spelling of "Widget_Idle". Change the order i.e start with "Idle" instead of "Widget_Idle" to see if it is a common problem (maybe wrong game object) or a Widget_Idle specific one.