下面的代碼工作完全正常,而我使用Unity 4.6團結5升級製造腳本從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變量。 (在C:/buildslave/unity/build/artifacts/generated/common/runtime/UnityEngineTransform.gen.cs:28) HadesController.FixedUpdate()(在Assets/Scripts/HadesController.cs中) :21)
在將它們添加到您的問題之前,請閱讀標記說明。 [tag:unity]!= [tag:unity3d] – 2015-03-31 20:05:13
從例外情況看,您的groundCheck字段中沒有設置檢查器。很難分辨出你發佈的內容。 – 2015-03-31 20:10:25