【发布时间】:2019-03-19 04:23:57
【问题描述】:
你好,我刚开始搞 Unity,这是我收到的第一条错误消息之一。我似乎无法理解它。
这是完整的消息
MissingComponentException:“玩家”游戏对象没有附加“刚体”,但脚本正在尝试访问它。 您可能需要将刚体添加到游戏对象“玩家”。或者您的脚本需要在使用之前检查组件是否已附加。 UnityEngine.Rigidbody.get_velocity () in :0 Player.Update () (在 Assets/Scripts/Player.cs:25)
这是我在 c# 中的播放器脚本
[SerializeField]
private Rigidbody playerBody;
private Vector3 inputVector;
private bool jump;
// Start is called before the first frame update
void Start()
{
playerBody = GetComponent<Rigidbody>();
}
// Update is called once per frame
void Update()
{
float speed = 10f;
inputVector = new Vector3(Input.GetAxis("Horizontal") * speed, playerBody.velocity.y, Input.GetAxis("Vertical") * speed);
transform.LookAt(transform.position + new Vector3(inputVector.x, 0, inputVector.z));
if (Input.GetButtonDown("Jump"))
{
jump = true;
}
}
private void FixedUpdate()
{
playerBody.velocity = inputVector;
if (jump)
{
playerBody.AddForce(Vector3.up * 20f, ForceMode.Impulse);
jump = false;
}
}
注意:
昨天我没有这个问题,我关闭了统一,今天当我打开它时,我遇到了这个问题。 不确定它是否与它有关......看起来很奇怪。
【问题讨论】:
-
您需要先将刚体组件附加到gameoject,然后才能通过脚本访问它。
-
但它是,不是`?我该如何检查?