当前位置: 动力学知识库 > 问答 > 编程问答 >

c# - Unity 5 Upgrade Made Script from Unity 4.6 stop work

问题描述:

The code below worked perfectly fine while I was using 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.

You probably need to assign the groundCheck variable of the HadesController script in the inspector.

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)

网友答案:

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.

That is, the Transform assigned to groundCheck somehow lost and now groundCheck is null. 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:

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

分享给朋友:
您可能感兴趣的文章:
随机阅读: