我用于弹药和碰撞的脚本不会让弹药用完
我试图建立一个团结的弹药系统,但这将行不通。问题在于,即使弹药用完了,我也可以继续破坏美学。
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的
ammo
永远不会变为零。您的脚本生命周期现在是:ammo
ammo
collision in Collision上的 10因此,您需要将
ammo
计算到一个地方,并使所有脚本都降低一个常见计数器,而不是每个对象的唯一脚本。例如,到创建子弹的脚本。这将更加合乎逻辑。Your
Ammo
will never become zero. Your script lifecycle now is:Ammo
to 10 on creationAmmo
on collision and destroy object withAmmo
count 9So 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.