【问题标题】:I get the error message: NullReferenceException: Object reference not set to an instance of an object. How do I fix it?我收到错误消息:NullReferenceException:对象引用未设置为对象的实例。我如何解决它?
【发布时间】:2017-04-26 06:41:26
【问题描述】:
    //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 项目的一部分,我对它有点陌生,所以请详细解释问题是什么以及如何解决它。我在这个网站上阅读了很多帖子,大部分问题都得到了解决,希望同样的事情发生在这里。爱你们!你是最棒的!我总是可以通过这个网站和非常友好的社区获得帮助。

【问题讨论】:

标签: javascript unity3d nullreferenceexception unity5


【解决方案1】:

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

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

使用调试器单步执行并检查变量。

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

检查“Widget_Idle”的拼写。更改顺序,即从“Idle”而不是“Widget_Idle”开始,看看这是一个常见问题(可能是错误的游戏对象)还是特定于 Widget_Idle 的问题。

【讨论】:

  • 是的,它实际上说编译器无法加载动画 Widget_Idle 检查了两次,因此名称正确,并且当我取消选中错误暂停时动画实际上在游戏模式下播放,这很奇怪故障和滞后很多。有什么想法吗?还是我需要重新加载?
猜你喜欢
  • 2019-08-18
  • 2015-04-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-11-05
  • 2017-02-24
  • 2013-04-19
相关资源
最近更新 更多