This question already has answers here:
Error1Operator '*' cannot be applied to operands of type 'method group' and 'double'
                                
                                    (5个答案)
                                
                        
                        
                            C# Operator '/' cannot be applied to operands of type 'method group' and 'int
                                
                                    (2个答案)
                                
                        
                        
                            Operator '==' cannot be applied to operands of type 'method group' and 'string'
                                
                                    (1个答案)
                                
                        
                        
                            Operator '>' cannot be applied to operands of type 'method group' and 'int'
                                
                                    (3个答案)
                                
                        
                                5个月前关闭。
            
                    
我为rpg制作了一个跳跃系统。 ->检测播放器何时接地的问题。

我试图使系统返回布尔值,并将其与if方法一起添加到jump方法中,不幸的是我卡住了

bool isGrounded ()
{
    return Physics.Raycast(transform.position, Vector3.down, distToGround);
}

//jump Force
if(Input.GetButton("Jump"))
{
    if(isGrounded == true)
    {
        GetComponent<Rigidbody>().AddForce (Vector3.up * 100);
    }
}


错误消息在这里。


  布尔isGrounded()
  运算符'=='不适用于'方法组'和'布尔'类型的操作数(CS0019)[Assembly-CSharp]

最佳答案

将其添加为答案,以便直到时间结束才出现在未回答的问题列表上

bool isGrounded ()
{
    return Physics.Raycast(transform.position, Vector3.down, distToGround);
}

//jump Force
if(Input.GetButton("Jump"))
{
    if(isGrounded == true)
    {
        GetComponent<Rigidbody>().AddForce (Vector3.up * 100);
    }
}


线

if(isGrounded == true)


告诉编译器找到一个名为isGrounded的符号,并将其值与true进行比较。由于isGrounded是一种方法,而不是布尔属性或字段,因此,您基本上是在要求编译器将isGrounded()true的地址进行比较,这完全是零含义(即使在C#中是允许的,事实并非如此)。

如果您将其更改为

if(isGrounded() == true)


或者,更简洁地说,

if(isGrounded())


它将调用isGrounded()并测试返回值。

括号很重要。

10-01 00:12