【问题标题】:Unity rigidbody refuses to exist despite global scope尽管有全局范围,但 Unity 刚体拒绝存在
【发布时间】:2019-12-05 07:37:23
【问题描述】:

我一直在尝试制作一个脚本,但是由于某种原因,我的 Rigidbody 变量拒绝存在于范围之外。它给我的错误读出:NullReferenceException: Object reference not set to an instance of an object VelocityBasedPlayerMovement.FixedUpdate () (at Assets/Scripts/VelocityBasedPlayerMovement.cs:80)

上一行的错误非常相似。

NullReferenceException: Object reference not set to an instance of an object VelocityBasedPlayerMovement.Update () (at Assets/Scripts/VelocityBasedPlayerMovement.cs:29)

这是我的代码。注意 Rigidbody 已设置,对象确实有一个刚体,因为第一个 debug.log 显示了它所在的对象的名称。但是这两行拒绝工作

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class VelocityBasedPlayerMovement : MonoBehaviour
{
    private Rigidbody rb;
    public int maxSpeed;
    public float speedMult;
    public float acceleration;
    public float decelleration;
    private bool keyNotPressed = false;
    private Vector3 velocity = new Vector3(0, 0, 0);
    private Vector3 temp = new Vector3(0, 0, 0);
    // Start is called before the first frame update
    void Start()
    {

        rb = gameObject.GetComponent<Rigidbody>();

        /*THIS IS FINE
        Debug.Log("Rigidbody attached to: "+rb.gameObject.name);
        */
        if(rb = null)
        {
            Debug.LogError("Could not find Rigid Body!\n" + this.name);
        }
    }

    // Update is called once per frame
    void Update()
    {
        /*THIS IS AN ERROR
        Debug.Log("Rigidbody attached to: " + rb.gameObject.name);
        */
        // TODO Make these proper controls instead of static keys

        if (Input.GetKeyDown(KeyCode.W))
        {
            keyNotPressed = false;
            temp += this.transform.forward;
        }
        if (Input.GetKeyDown(KeyCode.S))
        {
            keyNotPressed = false;
            temp += -this.transform.forward;
        }
        if (Input.GetKeyDown(KeyCode.A))
        {
            keyNotPressed = false;
            temp += -this.transform.right;
        }
        if (Input.GetKeyDown(KeyCode.D))
        {
            keyNotPressed = false;
            temp += this.transform.right;
        }
        else if(Input.GetKeyDown(KeyCode.None))
        {
            keyNotPressed = true;
            temp = new Vector3(0, 0, 0);
        }
        temp = temp.normalized;

    }

    void FixedUpdate()
    {
        if (!keyNotPressed)
        {
            if ((velocity + temp).magnitude > maxSpeed)
            {
                velocity = temp;
                Debug.LogWarning("Player is above max speed!\n" + (velocity + temp).magnitude);
            }
            else
            {
                velocity = temp*acceleration;
            }

        }
        else
        {
            velocity -= velocity * decelleration;
        }
       /*Error here too
       rb.velocity = velocity;
       */
    }

}

是的,我知道代码不应该按预期工作,但我正在努力。当刚体拒绝正确设置时,很难测试它

【问题讨论】:

    标签: c# unity3d


    【解决方案1】:

    你的错误是由这些行引起的

    if(rb = null)
    {
        Debug.LogError("Could not find Rigid Body!\n" + this.name);
    }
    

    您将 RigidBody 设置为 null,而不是对其进行 null 检查。

    将第一行替换为:

    if(rb == null)
    

    【讨论】:

    • Kieran,这种类型的问题有一个密切的原因(“由错字引起”)。因此,问题应该结束,而不是回答。
    • @Draco18s 我不知道这一点,感谢您的提醒。我不同意这个问题应该在没有答案或至少评论的情况下结束,尽管除了向他们抛出一个模糊的“由错字引起”之外,他们还能如何发现这个问题?
    • 对问题所在行的评论很好。例如"您可能需要仔细检查这一行:if(rb = null)"
    • 虽然我确定你是正确的,但我觉得这应该是一个答案,而不是 cmets 的用途。不管怎样,不是你的错!感谢您的提醒,我会确保下次这样做。
    猜你喜欢
    • 1970-01-01
    • 2014-09-13
    • 2022-01-02
    • 2022-01-13
    • 2018-12-27
    • 1970-01-01
    • 2015-05-13
    • 2022-11-29
    • 1970-01-01
    相关资源
    最近更新 更多