2015-03-31 83 views
-1

下面的代碼工作完全正常,而我使用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)

+0

在將它們添加到您的問題之前,請閱讀標記說明。 [tag:unity]!= [tag:unity3d] – 2015-03-31 20:05:13

+1

從例外情況看,您的groundCheck字段中沒有設置檢查器。很難分辨出你發佈的內容。 – 2015-03-31 20:10:25

回答

2

這是一個直截了當的錯誤。您只需要看到這部分例外情況:

您可能需要在檢查器中分配HadesController腳本的groundCheck變量。

也就是說,分配給地面檢查的變換以某種方式丟失,現在groundCheck爲空。你應該重新分配它。只需將先前分配的Transform(或gameobject)再次拖放到檢查器中的groundCheck即可。

只是錯誤行前添加一個調試檢查,你應該看它是否爲空或不:

的debug.log( 「groundCheck爲空:」 +(groundCheck == NULL));