Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/275.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/cocoa/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 没有刚体2D附加到游戏对象,但脚本正在尝试访问它_C#_Unity3d - Fatal编程技术网

C# 没有刚体2D附加到游戏对象,但脚本正在尝试访问它

C# 没有刚体2D附加到游戏对象,但脚本正在尝试访问它,c#,unity3d,C#,Unity3d,这是我正在使用的代码。它抛出一个MissingComponentException:没有刚体2D附加到“Bird”游戏对象,但脚本正在尝试访问它 using System.Collections; public class Bird : MonoBehaviour { public float UpForce; //Upward force of the "flap". private bool _isDead = fa

这是我正在使用的代码。它抛出一个
MissingComponentException
:没有刚体2D附加到“Bird”游戏对象,但脚本正在尝试访问它

using System.Collections;

public class Bird : MonoBehaviour 
{
    public float UpForce;                   //Upward force of the "flap".
    private bool _isDead = false;           //Has the player collided with a wall?

    private Animator _anim;                 //Reference to the Animator component.
    private Rigidbody2D _rb2d;              //Holds a reference to the Rigidbody2D component of the bird.

    void Start()
    {
        //Get reference to the Animator component attached to this GameObject.
        _anim = GetComponent<Animator> ();
        //Get and store a reference to the Rigidbody2D attached to this GameObject.
        _rb2d = GetComponent<Rigidbody2D>();
    }

    void Update()
    {
        //Don't allow control if the bird has died.
        if (_isDead == false) 
        {
            //Look for input to trigger a "flap".
            if (Input.GetMouseButtonDown(0)) 
            {
                //...tell the animator about it and then...
                _anim.SetTrigger("Flap");
                //...zero out the birds current y velocity before...
                _rb2d.velocity = Vector2.zero;
                //  new Vector2(rb2d.velocity.x, 0);
                //..giving the bird some upward force.
                _rb2d.AddForce(new Vector2(0, UpForce));
            }
        }
    }

    void OnCollisionEnter2D(Collision2D other)
    {
        // Zero out the bird's velocity
        _rb2d.velocity = Vector2.zero;
        // If the bird collides with something set it to dead...
        _isDead = true;
        //...tell the Animator about it...
        _anim.SetTrigger ("Die");
        //...and tell the game control about it.
        GameControl.instance.BirdDied ();
    }
}
使用系统集合;
公共类鸟类:单性行为
{
公共浮动向上力;//翻板的向上力。
private bool _isDead=false;//玩家是否与墙相撞?
私有Animator _anim;//对Animator组件的引用。
private rigiidbody2d _rb2d;//保存对bird的rigiidbody2d组件的引用。
void Start()
{
//获取附加到此游戏对象的Animator组件的引用。
_anim=GetComponent();
//获取并存储连接到此游戏对象的Rigidbody2D的引用。
_rb2d=GetComponent();
}
无效更新()
{
//如果鸟死了,就不要让它控制。
如果(_isDead==false)
{
//查找输入以触发“翻盖”。
if(Input.GetMouseButtonDown(0))
{
//…告诉动画师,然后。。。
_动画设置触发器(“活门”);
//…在…之前将鸟的电流y速度调零。。。
_rb2d.velocity=Vector2.0;
//新矢量2(rb2d.velocity.x,0);
//…给鸟一些向上的力量。
_rb2d.AddForce(新向量2(0,UpForce));
}
}
}
空心OnCollisionInter2D(碰撞2D其他)
{
//把鸟的速度调零
_rb2d.velocity=Vector2.0;
//如果这只鸟与什么东西相撞,就把它弄死。。。
_isDead=true;
//…告诉动画师这件事。。。
_动画设置触发(“死亡”);
//…并告诉游戏控制部。
GameControl.instance.birdied();
}
}

如何提供所需的引用?

此脚本附加到的游戏对象应具有RigidByD2D组件(因为脚本使用它向游戏对象应用力)。您可以在inspector中添加Rigidbody2D(选择脚本附加到的游戏对象并使用“添加组件”菜单),或者通过添加如下属性,使脚本在附加到游戏对象时自动添加组件:

[RequireComponent(typeof(Rigidbody2D))]
public class Bird : MonoBehaviour 
{
    //your Bird class code here
}
另请参见-这也是同样的问题