我遇到了参数空引发的异常错误。

huangapple go评论74阅读模式
英文:

I am getting argument null exception error

问题

这是我的代码:

using UnityEngine;

public class movement : MonoBehaviour
{
    public Rigidbody rb;
    // Start is called before the first frame update
    void Start()
    {

    }

    // Update is called once per frame
    void FixedUpdate()
    {
        rb.AddForce(0, 0, 2000 * Time.deltaTime);

    }
}

这是错误信息:

ArgumentNullException: 值不能为空。
参数名称:_unity_self
UnityEditor.SerializedObject.FindProperty (System.String propertyPath) (at <582c35e8f45345d395e99f8e72e3c16d>:0)
UnityEditor.UIElements.Bindings.SerializedObjectBindingContext.BindPropertyRelative (UnityEngine.UIElements.IBindable field, UnityEditor.SerializedProperty parentProperty) (at :0)
UnityEditor.UIElements.Bindings.SerializedObjectBindingContext.BindTree (UnityEngine.UIElements.VisualElement element, UnityEditor.SerializedProperty parentProperty) (at :0)
UnityEditor.UIElements.Bindings.SerializedObjectBindingContext.ContinueBinding (UnityEngine.UIElements.VisualElement element, UnityEditor.SerializedProperty parentProperty) (at :0)
UnityEditor.UIElements.Bindings.DefaultSerializedObjectBindingImplementation+BindingRequest.Bind (UnityEngine.UIElements.VisualElement element) (at :0)
UnityEngine.UIElements.VisualTreeBindingsUpdater.Update () (at :0)
UnityEngine.UIElements.VisualTreeUpdater.UpdateVisualTreePhase (UnityEngine.UIElements.VisualTreeUpdatePhase phase) (at :0)
UnityEngine.UIElements.Panel.UpdateBindings () (at :0)
UnityEngine.UIElements.UIElementsUtility.UnityEngine.UIElements.IUIElementsUtility.UpdateSchedulers () (at :0)
UnityEngine.UIElements.UIEventRegistration.UpdateSchedulers () (at :0)
UnityEditor.RetainedMode.UpdateSchedulers () (at :0)


<details>
<summary>英文:</summary>

This is my code:

using UnityEngine;

public class movement : MonoBehaviour
{
public Rigidbody rb;
// Start is called before the first frame update
void Start()
{

}

// Update is called once per frame
void FixedUpdate()
{
    rb.AddForce(0,0,2000 * Time.deltaTime);

}

}


This the error:

    ArgumentNullException: Value cannot be null.
    Parameter name: _unity_self
    UnityEditor.SerializedObject.FindProperty (System.String propertyPath) (at &lt;582c35e8f45345d395e99f8e72e3c16d&gt;:0)
    UnityEditor.UIElements.Bindings.SerializedObjectBindingContext.BindPropertyRelative (UnityEngine.UIElements.IBindable field, UnityEditor.SerializedProperty parentProperty) (at &lt;af95451922f042e9a8d1a10956fb36a2&gt;:0)
    UnityEditor.UIElements.Bindings.SerializedObjectBindingContext.BindTree (UnityEngine.UIElements.VisualElement element, UnityEditor.SerializedProperty parentProperty) (at &lt;af95451922f042e9a8d1a10956fb36a2&gt;:0)
    UnityEditor.UIElements.Bindings.SerializedObjectBindingContext.ContinueBinding (UnityEngine.UIElements.VisualElement element, UnityEditor.SerializedProperty parentProperty) (at &lt;af95451922f042e9a8d1a10956fb36a2&gt;:0)
    UnityEditor.UIElements.Bindings.DefaultSerializedObjectBindingImplementation+BindingRequest.Bind (UnityEngine.UIElements.VisualElement element) (at &lt;af95451922f042e9a8d1a10956fb36a2&gt;:0)
    UnityEngine.UIElements.VisualTreeBindingsUpdater.Update () (at &lt;d293f45b4ec64e6c9e762fe89794e7a5&gt;:0)
    UnityEngine.UIElements.VisualTreeUpdater.UpdateVisualTreePhase (UnityEngine.UIElements.VisualTreeUpdatePhase phase) (at &lt;d293f45b4ec64e6c9e762fe89794e7a5&gt;:0)
    UnityEngine.UIElements.Panel.UpdateBindings () (at &lt;d293f45b4ec64e6c9e762fe89794e7a5&gt;:0)
    UnityEngine.UIElements.UIElementsUtility.UnityEngine.UIElements.IUIElementsUtility.UpdateSchedulers () (at &lt;d293f45b4ec64e6c9e762fe89794e7a5&gt;:0)
    UnityEngine.UIElements.UIEventRegistration.UpdateSchedulers () (at &lt;d293f45b4ec64e6c9e762fe89794e7a5&gt;:0)
    UnityEditor.RetainedMode.UpdateSchedulers () (at &lt;af95451922f042e9a8d1a10956fb36a2&gt;:0)






</details>


# 答案1
**得分**: 3

`rb` 在你的代码中没有被赋值,也没有在Unity检视面板中设置。

```csharp
public class movement : MonoBehaviour
{
    public Rigidbody rb;
    // Start is called before the first frame update
    void Start()
    {
       rb = gameObject.GetComponent<Rigidbody>();
    }

    // 你的代码...
}
英文:

rb is not assigned in your code, or in the Unity inspector.

public class movement : MonoBehaviour
{
    public Rigidbody rb;
    // Start is called before the first frame update
    void Start()
    {
       rb = gameObject.GetComponent&lt;Rigidbody&gt;();
    }

    //your code....
}

huangapple
  • 本文由 发表于 2023年6月12日 20:44:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/76456796.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定