简体   繁体   English

来自Unity 4.6的Unity 5 Upgrade Made Script停止工作

[英]Unity 5 Upgrade Made Script from Unity 4.6 stop work

The code below worked perfectly fine while I was using Unity 4.6 当我使用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;
}
}

After I upgraded to Unity 5 it gave me this error message: UnassignedReferenceException: The variable groundCheck of HadesController has not been assigned. 升级到Unity 5之后,它给了我以下错误消息: UnassignedReferenceException:HadesController的变量groundCheck尚未分配。 You probably need to assign the groundCheck variable of the HadesController script in the inspector. 您可能需要在检查器中分配HadesController脚本的groundCheck变量。 UnityEngine.Transform.get_position () (at C:/buildslave/unity/build/artifacts/generated/common/runtime/UnityEngineTransform.gen.cs:28) HadesController.FixedUpdate () (at Assets/Scripts/HadesController.cs:21) UnityEngine.Transform.get_position()(在C:/buildslave/unity/build/artifacts/Generated/common/runtime/UnityEngineTransform.gen.cs:28)HadesController.FixedUpdate()(在Assets / Scripts / HadesController.cs:21 )

This is a straightforward error. 这是一个简单的错误。 You only need to see this part of the exception: 您只需要查看异常的这一部分:

You probably need to assign the groundCheck variable of the HadesController script in the inspector. 您可能需要在检查器中分配HadesController脚本的groundCheck变量。

That is, the Transform assigned to groundCheck somehow lost and now groundCheck is null. 也就是说,分配给groundCheck的Transform丢失了,现在groundCheck为空。 You should reassign it. 您应该重新分配它。 Just drag and drop previously assigned Transform (or gameobject) to the groundCheck in inspector again. 只需将先前分配的“变形”(或游戏对象)拖放到地面,再次在检查器中签入。

Add a debug check just before the error line and you should see if it's null or not: 在错误行之前添加调试检查,您应该查看它是否为null:

Debug.Log("groundCheck is null: " + (groundCheck == null)); Debug.Log(“ groundCheck为null:” +(groundCheck == null));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM