英文:
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
UnityEditor.UIElements.Bindings.SerializedObjectBindingContext.BindTree (UnityEngine.UIElements.VisualElement element, UnityEditor.SerializedProperty parentProperty) (at
UnityEditor.UIElements.Bindings.SerializedObjectBindingContext.ContinueBinding (UnityEngine.UIElements.VisualElement element, UnityEditor.SerializedProperty parentProperty) (at
UnityEditor.UIElements.Bindings.DefaultSerializedObjectBindingImplementation+BindingRequest.Bind (UnityEngine.UIElements.VisualElement element) (at
UnityEngine.UIElements.VisualTreeBindingsUpdater.Update () (at
UnityEngine.UIElements.VisualTreeUpdater.UpdateVisualTreePhase (UnityEngine.UIElements.VisualTreeUpdatePhase phase) (at
UnityEngine.UIElements.Panel.UpdateBindings () (at
UnityEngine.UIElements.UIElementsUtility.UnityEngine.UIElements.IUIElementsUtility.UpdateSchedulers () (at
UnityEngine.UIElements.UIEventRegistration.UpdateSchedulers () (at
UnityEditor.RetainedMode.UpdateSchedulers () (at
<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 <582c35e8f45345d395e99f8e72e3c16d>:0)
UnityEditor.UIElements.Bindings.SerializedObjectBindingContext.BindPropertyRelative (UnityEngine.UIElements.IBindable field, UnityEditor.SerializedProperty parentProperty) (at <af95451922f042e9a8d1a10956fb36a2>:0)
UnityEditor.UIElements.Bindings.SerializedObjectBindingContext.BindTree (UnityEngine.UIElements.VisualElement element, UnityEditor.SerializedProperty parentProperty) (at <af95451922f042e9a8d1a10956fb36a2>:0)
UnityEditor.UIElements.Bindings.SerializedObjectBindingContext.ContinueBinding (UnityEngine.UIElements.VisualElement element, UnityEditor.SerializedProperty parentProperty) (at <af95451922f042e9a8d1a10956fb36a2>:0)
UnityEditor.UIElements.Bindings.DefaultSerializedObjectBindingImplementation+BindingRequest.Bind (UnityEngine.UIElements.VisualElement element) (at <af95451922f042e9a8d1a10956fb36a2>:0)
UnityEngine.UIElements.VisualTreeBindingsUpdater.Update () (at <d293f45b4ec64e6c9e762fe89794e7a5>:0)
UnityEngine.UIElements.VisualTreeUpdater.UpdateVisualTreePhase (UnityEngine.UIElements.VisualTreeUpdatePhase phase) (at <d293f45b4ec64e6c9e762fe89794e7a5>:0)
UnityEngine.UIElements.Panel.UpdateBindings () (at <d293f45b4ec64e6c9e762fe89794e7a5>:0)
UnityEngine.UIElements.UIElementsUtility.UnityEngine.UIElements.IUIElementsUtility.UpdateSchedulers () (at <d293f45b4ec64e6c9e762fe89794e7a5>:0)
UnityEngine.UIElements.UIEventRegistration.UpdateSchedulers () (at <d293f45b4ec64e6c9e762fe89794e7a5>:0)
UnityEditor.RetainedMode.UpdateSchedulers () (at <af95451922f042e9a8d1a10956fb36a2>: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<Rigidbody>();
}
//your code....
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论