简体   繁体   English

如何解决错误:运算符“==”不能应用于“方法组”和“int”类型的操作数

[英]How do I solve error: Operator '==' cannot be applied to operands of type 'method group' and 'int'

I have game when you are destroying boxes with ball witch you are bouncing with platform (standard 2d game).当你用你在平台上弹跳的球女巫摧毁盒子时,我有游戏(标准 2d 游戏)。 And I have power ups that are spawning and they are multiply number of balls and when ball touch a wall that is downstair its destroyed.而且我有正在生成的电源,它们是球的倍数,当球碰到楼下的墙壁时,它被摧毁了。 I have program where is when number of balls is multyplyed it sets number of balls +1 but one thing doesn't work right.我有一个程序,当球的数量被乘以它时,它会设置球的数量+1,但有一件事不能正常工作。

SCRIPT ON BALL:球上的脚本:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ball2 : MonoBehaviour
{
private Rigidbody2D rb;
public GameObject ball;

[SerializeField]
private int forceOne = 10;

[SerializeField]
private int forceTwo = 15;
private float balls;
public float numBalls = 1;

void Start()
{
    rb = GetComponent<Rigidbody2D>();
    rb.AddForce(new Vector2(forceOne * 25f, forceTwo * 25f));
    balls = numBalls;
}

public void Copy()
{
    Instantiate(ball, this.transform.position, Quaternion.identity);
    balls++;
}

} }

and I'm trying to connect this with my script on platform witch is:我正在尝试将其与平台上的脚本联系起来,女巫是:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;

public class movement : MonoBehaviour
{
[SerializeField]
private float speed = 10f;
public int startBoxes = 120;
private int boxes;
private string MUL_TAG = "mul";

private float movementX;

private void Start()
{
    boxes = startBoxes;
}

private void Update()
{
    Movement();
}

public void Destroy()
{
    boxes--;                                            

    if (boxes <= 0)
        endGame();
}

private void endGame()
{
    SceneManager.LoadScene("level_");
}

private void Movement()
{
    movementX = Input.GetAxisRaw("Horizontal");
    transform.position += new Vector3 (movementX, 0f, 0f) * Time.deltaTime * speed;
}

private void OnCollisionEnter2D(Collision2D collision)
{
    if (collision.gameObject.CompareTag(MUL_TAG))
    {
        ball2 PBDM = FindObjectOfType<ball2>();
        PBDM.Copy();
    }



}

public void balls()
{
    
    if (balls <= 0)
        SceneManager.LoadScene("loseScreen");

}

} }

but on this last part on balls function its an error: Operator '<=' cannot be applied to operands of type 'method group' and 'int'.但在球函数的最后一部分中,它有一个错误:运算符'<='不能应用于'method group'和'int'类型的操作数。 I tryed to change int into float but it doesn't work.我试图将 int 更改为 float 但它不起作用。 So my point is when all balls are destroyed you lose.所以我的观点是,当所有的球都被摧毁时,你就输了。

PS I want to know if this: ball2 PBDM = FindObjectOfType(); PS我想知道是否:ball2 PBDM = FindObjectOfType(); is PBDM you can changes it or its actualy a think? PBDM 你可以改变它还是它实际上是一个想法? For example:例如:

ball2 TEXT = FindObjectOfType(); ball2 TEXT = FindObjectOfType(); TEXT.Copy();文本.复制();

There is no variable balls declared anywhere in the movement class.movement类的任何地方都没有声明变量balls What, then, is it trying to compare with <= ?那么,它试图与<=比较什么? Well, the only thing that you've declared with that name is a method called balls .好吧,您使用该名称声明的唯一内容是一个名为balls的方法。 Methods are not numbers, they are methods.方法不是数字,它们是方法。

The variable balls is only defined as a private variable within the 'ball2' class.变量balls仅定义为 'ball2' 类中的私有变量。 This is not accessible to anything except for that instance of the 'ball2' class.除了“ball2”类的那个实例之外,任何东西都无法访问它。 As a side note, if you run 'Copy()' on the ball, each new ball will get a separate instance of the 'balls' variable.附带说明一下,如果您在球上运行“Copy()”,每个新球将获得一个单独的“balls”变量实例。 They won't be the same value.它们不会是相同的值。 You could create a game manager prefab, and make that copy and keep track of the balls instead.您可以创建一个游戏管理器预制件,然后制作该副本并跟踪球。 This is probably the better and more standard way to solve this issue.这可能是解决此问题的更好、更标准的方法。 There are guides on Singletons and Game manager prefabs like this one , but here's an example:有关于 Singletons 和 Game manager prefabs 这样的指南,但这里有一个例子:

public class GameManager : MonoBehaviour
{
    private static _instance = null;
    public static GameManager Instance
    { 
        get 
        {
            if (_instance == null)
            {
                _instance = this;
            }
            else
            {
                Destroy(gameObject);
            }

            return _instance;
        }
    }

    private int _balls;
    public int Balls 
    { 
        get => _balls;

        set
        {
            int oldValue = _balls;
            _balls = value;
            OnBallsChanged(oldValue);
        }
    }

    private void OnBallsChanged (int oldValue)
    {
        if (Balls <= 0)
        {
            SceneManager.LoadScene("loseScreen");
        }
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 运算符“&gt;”不能应用于类型“方法组”和“整数”的操作数 - Operator '>' cannot be applied to operands of type 'method group' and 'int' 运算符不能应用于'Method Group'和'int'类型的操作数 - Operator cannot be applied to operands of type 'Method Group' and 'int' C#运算符&#39;/&#39;不能应用于&#39;方法组&#39;和&#39;int类型的操作数 - C# Operator '/' cannot be applied to operands of type 'method group' and 'int 运算符“&gt;”不能应用于“int”和“方法组”类型的操作数 - Operator '>' cannot be applied to operands of type 'int' and 'method group' 错误1运算符&#39;&gt; =&#39;不能应用于类型为&#39;方法组&#39;和&#39;方法组&#39;的操作数 - Error 1 Operator '>=' cannot be applied to operands of type 'method group' and 'method group' 运算符 &lt; 不能应用于“方法组”类型的操作数 - Operator < cannot be applied to operands of type "method group" 错误消息:运算符“ +”不能应用于类型为“字符串”和“方法组”的操作数 - Error Message: Operator '+' cannot be applied to operands of type 'string' and 'method group' 错误1运算符&#39;*&#39;不能应用于&#39;方法组&#39;和&#39;双精度&#39;类型的操作数 - Error 1 Operator '*' cannot be applied to operands of type 'method group' and 'double' Fade方法中Operator '*' cannot be applyed to operands of type 'float' and 'bool'如何解决? - How to solve the error Operator '*' cannot be applied to operands of type 'float' and 'bool' in the Fade method? 运算符“ ==”不能应用于“方法组”和“方法组”类型的操作数 - Operator `==' cannot be applied to operands of type `method group' and `method group'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM