如果发生了什么事,该怎么做-1
当我摧毁所有盒子时,我正在尝试做一些事情。 我的代码是;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class destroy : MonoBehaviour
{
private string BALL_TAG = "ball";
public AudioClip coin;
public AudioSource src;
public float numBox = 120f;
public bool isDestroyed;
private void OnCollisionEnter2D(Collision2D collision)
{
if (collision.gameObject.CompareTag(BALL_TAG))
{
src.clip = coin;
src.Play();
Destroy(gameObject);
isDestroyed = true;
}
}
private void Update()
{
boxes();
}
public void boxes()
{
if(isDestroyed == true)
numBox -= 1f;
if(numBox == 119)
SceneManager.LoadScene("mainManu");
}
private IEnumerator Two()
{
yield return new WaitForSeconds(1f);
Destroy(gameObject);
}
}
但它行不通。 当我打破1个盒子时,它将我送到菜单时会这样做。 我认为它在“ numbox- = 1f;”中的问题因为我不知道做这个。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我完全不了解您的代码。因此,我需要做一些假设。
我认为脚本附加到框上,每个盒子都有此脚本。我还认为,您的球员会射击。这些球有一个带有球标签的对撞机。
您的代码有多个问题。
第一个是,您的计数变量numbox保存在您的销毁脚本中,该脚本放在每个盒子上。
这意味着,每个盒子都在计算自己。
您必须集中这个。有多种方法可以做到这一点。
一种方法是将此变量声明为静态()
这不是最好的实践,而是起作用。
一个更好的方法是在您的播放器上拥有一个脚本,该脚本容纳此数字,并且每个盒子都搜索此脚本,并在被销毁时更改此数字。
第二个大问题是,您在更新中做了一些非常奇怪的事情,
首先是碰撞处理,您将
iSdestroyed
设置为true。然后,在每个帧中调用的框方法中,您将numbox
变量降低一个,如果被销毁是正确的。因此,如果您的盒子被击中,您将减少每个框架。
之后,您正在检查每个帧,如果您的numbox为119
如果是这样,您会更改场景。
这就是原因,为什么只有一个男孩
这个行为很奇怪,因为这是完全不必要的。您可以在OnCollisionEnter2D方法中直接减少变量。
有一些小事情,可以改进。
I don't understand your code completely. So, I need to make some assumptions.
I think the Script is attached to the box and every box has this Script. I also think, that your player Shoots Ball. Those Balls have a collider with an ball tag.
There are multiple problems with your code.
The first one is, that your count variable, numBox, is saved in your destroy Script, which is placed on each box.
this means, that every Box is counting for itself.
You have to centralize this. There are multiple ways for doing this.
One way is to declare this variable as static(https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/static)
This is not best practice, but works.
A Better way is to have a Script on Your Player, which holds this number and every Box searches for this Script and change this number if it is destroyed.
The second big Problem is, that your are doing some really weird thing in your Update and the collision handling
First of all, you are setting
isDestroyed
to true. Then in your boxes method, which is called in every Frame, you are decrementing yournumBox
variable by one, if this is Destroyed is true.So if your Box gets hit, you are decrementing every frame.
After that you are checking every frame if your numBox is 119
If so, you change the Scene.
This is the reason, why you are getting to your MainMenu after only one boy
This behaviour is very weird, because it is totally unnecessary. You can reduce your variable directly in in your OnCollisionEnter2D Method.
There are some little things, which can be improved.