unity-game-engine - C#统一: Cannot convert method group 'GetComponent' to non-delegate type ' Rigidbody2D'

标签 unity-game-engine

我是 C#/C++ 新手(很抱歉,如果代码中的错误很明显。)

我正在使用this tutorial (1:27:47 标记)并且遇到了错误。我尝试通过在网络上查找其他遇到类似问题的人的解决方案来修复此代码。当我第一次得到这段代码时,错误是:

UnityEngine.Component”不包含“velocity”的定义,并且找不到“UnityEngine.Component”类型的扩展方法“velocity”。您是否缺少程序集引用?'

应用一些修复后,代码现在如下所示:

using UnityEngine;
using System.Collections;
using UnityEngine.UI;

//This scrits makes the character move when the screen is pressed and 
handles the jump
public class CharacterFinal : MonoBehaviour
{
public bool jump = false;               // Condition for whether the player should jump.    
public float jumpForce = 10.0f;         // Amount of force added when the player jumps.
private bool grounded = false;          // Whether or not the player is grounded.
public int movementSpeed = 10;          // The vertical speed of the movement
private Animator anim;                  // The animator that controls the characters animations
//Declare rigid2D
Rigidbody2D rigid2D;
// Use this for initialization

void Awake()
{
    anim = GetComponent<Animator>();

    //Initialize rigid2D
    rigid2D = GetComponent<Rigidbody2D>;
}


//This method is called when the character collides with a collider (could be a platform).
void OnCollisionEnter2D(Collision2D hit)
{
    grounded = true;
    print ("isground");
}

//The update method is called many times per seconds
void Update()
{
    if(Input.GetButtonDown("Fire1"))
    {       
        // If the jump button is pressed and the player is grounded and the character is running forward then the player should jump.
        if(grounded == true)                        
        {
            jump = true;
            grounded = false;
            //We trigger the Jump animation state
            anim.SetTrigger("Jump");
        }

    }

}




//Since we are using physics for movement, we use the FixedUpdate method
void FixedUpdate ()
{

    //if died that 
    rigid2D.velocity = new Vector2(movementSpeed, rigid2D.velocity.y );
    //else
    //moving


    // If jump is set to true we add a quick force impulse for the jump
    if(jump == true)
    {
        // Add a vertical force to the player.
        rigid2D.AddForce(new Vector2(0f, jumpForce),ForceMode2D.Impulse);

        // We set the variable to false again to avoid adding force constantly
        jump = false;
    }
}

}

它给出的错误是这样的

Cannot convert method group 'GetComponent' to non-delegate type 
UnityEngine.Rigidbody2D'.

错误出现在第22行

rigid2D = GetComponent<Rigidbody2D>;

最佳答案

简单地改变

rigid2D = GetComponent<Rigidbody2D>;

rigid2D = GetComponent<Rigidbody2D>();

注意最后的()。该错误表示它无法转换“方法组” - 那是因为它是一个方法,但您没有正确调用它。

关于unity-game-engine - C#统一: Cannot convert method group 'GetComponent' to non-delegate type ' Rigidbody2D' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52910746/

相关文章:

c# - 对固定 block 内的指针所做的更改是否保留?

unity-game-engine - Unity 上的等待功能

java - 多线程套接字服务器 - 客户端相互通信

c# - 更改启动屏幕/启动屏幕颜色和图像

c# - 在第二个脚本上调用时 Unity 中的静态值 "lost"

colors - 在运行时更改文本颜色(Unity)?

c# - 如何等待协程回调执行?

unity-game-engine - 需要以特定方式旋转物体 - 被万向节锁卡住?

c# - 如何顺利变道

android - Unity Gradle Multidex 支持不起作用