英文:
How to fix error 'Rigidbody2D' does not contain a definition for 'Static' in Unity 2D game engine?
问题
我正在跟随一个教程,但当我保存Unity工程时,由于错误,无法测试我的工作。完整的错误信息是:“Assets\scripts\VidaPlayer.cs(25,34): error CS0117: 'Rigidbody2D' does not contain a definition for 'Static'”。它只在Unity中检测到错误,在Visual Studio中不显示任何错误。
这是我写的脚本:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class LifePlayer : MonoBehaviour
{
private Animator anim;
private Rigidbody2D rb;
private void Start()
{
anim= GetComponent<Animator>();
rb= GetComponent<Rigidbody2D>();
}
private void OnCollisionEnter2D(Collision2D collision)
{
if (collision.gameObject.CompareTag("Trap"))
{
Death();
}
}
private void Death()
{
rb.bodyType = Rigidbody2DType2D.Static;//<-- 这里显示错误
anim.SetTrigger("death");
}
}
我尝试删除与rb相关的所有内容,但它显示这个错误:
NullReferenceException: Object reference not set to an instance of an object
UnityEditor.Graphs.Edge.WakeUp () (at <8e8a32ebae4d4c768ca69b7e552734c1>:0)
UnityEditor.Graphs.Graph.DoWakeUpEdges (System.Collections.Generic.List1[T] inEdges, System.Collections.Generic.List
1[T] ok, System.Collections.Generic.List`1[T] error, System.Boolean inEdgesUsedToBeValid) (at <8e8a32ebae4d4c768ca69b7e552734c1>:0)
UnityEditor.Graphs.Graph.WakeUpEdges (System.Boolean clearSlotEdges) (at <8e8a32ebae4d4c768ca69b7e552734c1>:0)
UnityEditor.Graphs.Graph.WakeUp (System.Boolean force) (at <8e8a32ebae4d4c768ca69b7e552734c1>:0)
UnityEditor.Graphs.Graph.WakeUpSlot (System.Int32 slot, System.Boolean force) (at <8e8a32ebae4d4c768ca69b7e552734c1>:0)
UnityEditor.Graphs.GraphEditor.ScheduleNodeRebuild (UnityEditor.Graphs.Graph[] graphs, UnityEditor.Graphs.Node[] nodes) (at <8e8a32ebae4d4c768ca69b7e552734c1>:0)
UnityEditor.Graphs.GraphEditor.BuildContextChanged () (at <8e8a32ebae4d4c768ca69b7e552734c1>:0)
UnityEditor.Graphs.GraphEditor.DelayedBuild () (at <8e8a32ebae4d4c768ca69b7e552734c1>:0)
UnityEditor.Graphs.GraphEditor.BuildContextChanged () (at <8e8a32ebae4d4c768ca69b7e552734c1>:0)
UnityEditor.Graphs.GraphEditor.DelayedBuild () (at <8e8a32ebae4d4c768ca69b7e552734c1>:0)
UnityEditor.Graphs.GraphEditor.Rebuild () (at <8e8a32ebae4d4c768ca69b7e552734c1>:0)
UnityEditor.Graphs.GraphEditor.OnGUI () (at <8e8a32ebae4d4c768ca69b7e552734c1>:0)
UnityEngine.GUIUtility:ProcessEvent(Int32, IntPtr)
<details>
<summary>英文:</summary>
im following a [tutorial](https://www.youtube.com/watch?v=ynH51MiKutY&list=PLrnPJCHvNZuCVTz6lvhR81nnaf1a-b67U&index=8) and when i saved the Unity Engine didnt allow me to test my work because of an error. the full error says "Assets\scripts\VidaPlayer.cs(25,34): error CS0117: 'Rigidbody2D' does not contain a definition for 'Static' ". It only detects the error on unity, in VS Studio doesnt show any error.
This is the script that i wrote:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class LifePlayer : MonoBehaviour
{
private Animator anim;
private Rigidbody2D rb;
private void Start()
{
anim= GetComponent<Animator>();
rb= GetComponent<Rigidbody2D>();
}
private void OnCollisionEnter2D(Collision2D collision)
{
if (collision.gameObject.CompareTag("Trap"))
{
Death();
}
}
private void Death()
{
rb.bodyType = Rigidbody2D.Static;//<-- where it shows the error
anim.SetTrigger("death");
}
}
i tried to remove all that is involved with the rb but it show me this error :
NullReferenceException: Object reference not set to an instance of an object
UnityEditor.Graphs.Edge.WakeUp () (at <8e8a32ebae4d4c768ca69b7e552734c1>:0)
UnityEditor.Graphs.Graph.DoWakeUpEdges (System.Collections.Generic.List`1[T] inEdges, System.Collections.Generic.List`1[T] ok, System.Collections.Generic.List`1[T] error, System.Boolean inEdgesUsedToBeValid) (at <8e8a32ebae4d4c768ca69b7e552734c1>:0)
UnityEditor.Graphs.Graph.WakeUpEdges (System.Boolean clearSlotEdges) (at <8e8a32ebae`your text`4d4c768ca69b7e552734c1>:0)
UnityEditor.Graphs.Graph.WakeUp (System.Boolean force) (at <8e8a32ebae4d4c768
</details>
# 答案1
**得分**: 0
代码部分不要翻译。以下是翻译好的内容:
"everything seems working fine in the code, Try exiting and restarting Unity if you open the player animator you will notice that it have disappeared this is a bug in unity
try restarting unity it should work again as intended"
<details>
<summary>英文:</summary>
everything seems working fine in the code , Try exiting and restarting Unity if you open the player animator you will notice that it have disappeared this is a bug in unity
try restarting unity it should work again as intended
</details>
# 答案2
**得分**: 0
在代码中:
将以下行:
rb.bodyType = Rigidbody2D.Static;//<-- where it shows the error
更改为:
rb.bodyType = RigidbodyType2D.Static;
<details>
<summary>英文:</summary>
Simple small typo
in Code :
rb.bodyType = Rigidbody2D.Static;//<-- where it shows the error
change to:
rb.bodyType = Rigidbody**Type**2D.Static;
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论