我用于弹药和碰撞的脚本不会让弹药用完

发布于 2025-02-11 17:51:28 字数 676 浏览 1 评论 0原文

我试图建立一个团结的弹药系统,但这将行不通。问题在于,即使弹药用完了,我也可以继续破坏美学。

using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class ExplosionOnTouch : MonoBehaviour
{
    // Start is called before the first frame update
    public int Ammo = 10;
    void Start()
    {

    }

    // Update is called once per frame
    void Update()
    {

    }
    private void OnCollisionEnter2D(Collision2D collision)
    {
        if (Ammo > 0)
        {
            Ammo = Ammo - 1;
            Destroy(gameObject);
        }
        else
        {
        SceneManager.LoadScene("DeathScreen", LoadSceneMode.Single);
        }
    }
}

I am trying to make an ammo system in unity but it will not work. The Problem Is That I Can Keep Destroying The Aestroid Even When My Ammo Runs Out.

using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class ExplosionOnTouch : MonoBehaviour
{
    // Start is called before the first frame update
    public int Ammo = 10;
    void Start()
    {

    }

    // Update is called once per frame
    void Update()
    {

    }
    private void OnCollisionEnter2D(Collision2D collision)
    {
        if (Ammo > 0)
        {
            Ammo = Ammo - 1;
            Destroy(gameObject);
        }
        else
        {
        SceneManager.LoadScene("DeathScreen", LoadSceneMode.Single);
        }
    }
}

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

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

发布评论

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

评论(1

站稳脚跟 2025-02-18 17:51:28

您的ammo永远不会变为零。您的脚本生命周期现在是:

  • 创建对象
  • ammo
  • 在Collision时创建降低ammo collision in Collision上的 10

因此,您需要将ammo计算到一个地方,并使所有脚本都降低一个常见计数器,而不是每个对象的唯一脚本。例如,到创建子弹的脚本。这将更加合乎逻辑。

Your Ammo will never become zero. Your script lifecycle now is:

  • create an object
  • set Ammo to 10 on creation
  • decrease Ammo on collision and destroy object with Ammo count 9

So you need to move your Ammo count to one place and to make all scripts decrease one common counter instead of unique for every object. For example, to the script that creates your bullets. It will be much more logical.

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