如何修复在Unity 2D游戏引擎中出现的错误’Rigidbody2D’不包含’Static’的定义?

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

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.List1[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&amp;list=PLrnPJCHvNZuCVTz6lvhR81nnaf1a-b67U&amp;index=8) and when i saved the Unity Engine didnt allow me to test my work because of an error. the full error says &quot;Assets\scripts\VidaPlayer.cs(25,34): error CS0117: &#39;Rigidbody2D&#39; does not contain a definition for &#39;Static&#39; &quot;.  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&lt;Animator&gt;(); 
     rb= GetComponent&lt;Rigidbody2D&gt;();
}

 private void OnCollisionEnter2D(Collision2D collision) 
 {
     if (collision.gameObject.CompareTag(&quot;Trap&quot;))
     {
        Death();
     }
 }

 private void Death()
 {
      rb.bodyType = Rigidbody2D.Static;//&lt;-- where it shows the error
      anim.SetTrigger(&quot;death&quot;);
 }

}



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 &lt;8e8a32ebae4d4c768ca69b7e552734c1&gt;: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 &lt;8e8a32ebae4d4c768ca69b7e552734c1&gt;:0)
UnityEditor.Graphs.Graph.WakeUpEdges (System.Boolean clearSlotEdges) (at &lt;8e8a32ebae`your text`4d4c768ca69b7e552734c1&gt;:0)
UnityEditor.Graphs.Graph.WakeUp (System.Boolean force) (at &lt;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;//&lt;-- where it shows the error

更改为:

rb.bodyType = RigidbodyType2D.Static;

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

Simple small typo

in Code :

    rb.bodyType = Rigidbody2D.Static;//&lt;-- where it shows the error

change to:

rb.bodyType = Rigidbody**Type**2D.Static;



</details>



huangapple
  • 本文由 发表于 2023年5月29日 02:02:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/76352907.html
匿名

发表评论

匿名网友

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

确定