我收到错误消息:NullReferenceException:对象引用未设置为对象的实例。我该如何解决?

时间:2016-12-11 05:13:25

标签: javascript unity3d nullreferenceexception unity5

    //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")

这是我的代码,如果你不想,你不需要看整件事。发生错误的关键行是这一行,也可以从错误消息中得知为第14行。

((GetComponent。()as Animation)[“Widget_Idle”] as AnimationClip).layer = -1;

所以任何人都可以帮忙,因为我坚持了一个星期,现在什么都不做,除了搜索互联网,特别是这个网站,特别是一无所获。上面的脚本是Unity3D项目的一部分,我是一个新手,所以请详细解释问题是什么以及如何解决它。我在这个网站上看了很多帖子,大部分问题都解决了,希望在这里发生同样的事情。爱你们!你是最棒的!我总是可以依靠这个网站的帮助和非常友好的社区。

1 个答案:

答案 0 :(得分:0)

将这条线分成几个小步骤以缩小原因,即:

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

使用调试器逐步检查变量。

检查控制台中的每一行,单击它以查看是否在编辑器中突出显示了正确的游戏对象(前提是您已将其指定为第二个参数)。

检查“Widget_Idle”的拼写。更改顺序,即以“空闲”而不是“Widget_Idle”开头,以查看它是否是常见问题(可能是错误的游戏对象)或Widget_Idle特定问题。