如何求解错误:	运算符' =='不能应用于类型的操作数'和' int'

发布于 2025-02-01 21:23:43 字数 2067 浏览 4 评论 0原文

当您用球女巫销毁盒子时,我有游戏(标准2D游戏)。而且我有产卵的功率上升,它们是乘数的倍数,当球碰到脚下被摧毁的墙壁时。我有程序在哪里是多型球的数量,它设置了球数+1,但一件事不起作用。

球上的脚本:

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++;
}

}

,我正在尝试将其与我在平台女巫上的脚本联系起来:

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");

}

}

但是,在Balls函数上的最后一部分上,它是一个错误:操作员'&lt; ='不能应用于类型的操作数'方法组'和'int'。 我试图将int更改为float,但它不起作用。 所以我的观点是,当所有球被摧毁时,你输了。

PS我想知道是否这样:Ball2 PBDM = FindObjectOftype(); PBDM您可以更改它还是实际的想法?例如:

ball2 text = findObjectOftype(); text.copy();

I have game when you are destroying boxes with ball witch you are bouncing with platform (standard 2d game). 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.

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'.
I tryed to change int into float but it doesn't work.
So my point is when all balls are destroyed you lose.

P.S I want to know if this: ball2 PBDM = FindObjectOfType();
is PBDM you can changes it or its actualy a think? For example:

ball2 TEXT = FindObjectOfType();
TEXT.Copy();

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

你又不是我 2025-02-08 21:23:43

变量仅定义为“ Ball2”类中的私人变量。除了“ ball2”类的实例之外,任何东西都无法访问。作为旁注,如果您在球上运行“ copy()”,则每个新球将获得“球”变量的单独实例。它们不会具有相同的价值。您可以创建游戏经理预制,并制作该副本并跟踪球。这可能是解决此问题的更好,更标准的方法。有指南和游戏经理预制器喜欢这个,但这是一个例子:

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");
        }
    }
}

The variable balls is only defined as a private variable within the 'ball2' class. This is not accessible to anything except for that instance of the 'ball2' class. As a side note, if you run 'Copy()' on the ball, each new ball will get a separate instance of the 'balls' variable. 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:

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");
        }
    }
}
转身泪倾城 2025-02-08 21:23:43

没有变量运动类中的任何地方声明。那么,它试图与&lt; =进行比较?好吧,您用该名称声明的唯一一件事是一种称为Balls的方法。方法不是数字,它们是方法。

There is no variable balls declared anywhere in the movement class. What, then, is it trying to compare with <=? Well, the only thing that you've declared with that name is a method called balls. Methods are not numbers, they are methods.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文