Unity 5升级Made Unity中的脚本停止工作

时间:2015-03-31 19:54:39

标签: c# unity3d

当我使用Unity 4.6

时,下面的代码完全正常
using UnityEngine;
using System.Collections;
public class HadesController : MonoBehaviour {
public float maxSpeed = 10f;
bool facingRight = true;

Animator anim;

bool grounded = false;
public Transform groundCheck;
float groundRadius = 0.2f;
public LayerMask WhatIsGround;
public float jumpForce = 700f;

void Start ()
{
    anim = GetComponent<Animator>();
}
void FixedUpdate ()
{
    grounded = Physics2D.OverlapCircle (groundCheck.position, groundRadius, WhatIsGround);
    anim.SetBool ("Ground", grounded);
    anim.SetFloat ("vSpeed", GetComponent<Rigidbody2D>().velocity.y);
    if (!grounded)
                    return;
    float move = Input.GetAxis ("Horizontal");
    anim.SetFloat("Speed", Mathf.Abs (move));
    GetComponent<Rigidbody2D>().velocity = new Vector2 (move * maxSpeed, GetComponent<Rigidbody2D>().velocity.y);
    if (move > 0 &&!facingRight)
        Flip ();
    else if (move <0 && facingRight)
        Flip ();
}
void Update()
{
    if (grounded && Input.GetKeyDown (KeyCode.Space)) 
    {
        anim.SetBool("Ground", false);
        GetComponent<Rigidbody2D>().AddForce(new Vector2(0, jumpForce));
    }
}
void Flip()
{
    facingRight = !facingRight;
    Vector3 theScale = transform.localScale;
    theScale.x *= -1;
    transform.localScale = theScale;
}
}

升级到Unity 5后,它给了我这个错误消息: UnassignedReferenceException:尚未分配HadesController的变量groundCheck。 您可能需要在检查器中分配HadesController脚本的groundCheck变量。 UnityEngine.Transform.get_position()(在C:/buildslave/unity/build/artifacts/generated/common/runtime/UnityEngineTransform.gen.cs:28) HadesController.FixedUpdate()(在Assets / Scripts / HadesController.cs:21)

1 个答案:

答案 0 :(得分:2)

这是一个简单的错误。您只需要看到异常的这一部分:

  

您可能需要在检查器中分配HadesController脚本的groundCheck变量。

也就是说,分配给groundCheck的Transform以某种方式丢失了,现在groundCheck为null。你应该重新分配它。只需将先前分配的变换(或游戏对象)拖放到检查器中的groundCheck即可。

在错误行之前添加调试检查,您应该看看它是否为空:

  

Debug.Log(&#34; groundCheck为null:&#34; +(groundCheck == null));