如果发生了什么事,该怎么做-1

发布于 2025-01-31 09:13:27 字数 942 浏览 4 评论 0 原文

当我摧毁所有盒子时,我正在尝试做一些事情。 我的代码是;

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;”中的问题因为我不知道做这个。

I am trying to do when i destroy all boxes something happen.
My code is;

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

}

But it doesn't work.
It is suppose to do when I broke 1 box it sends me to menu.
I think its problem in "numBox -= 1f;" because I don't know hot to make this.

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

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

发布评论

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

评论(1

少女情怀诗 2025-02-07 09:13:27

我完全不了解您的代码。因此,我需要做一些假设。
我认为脚本附加到框上,每个盒子都有此脚本。我还认为,您的球员会射击。这些球有一个带有球标签的对撞机。

您的代码有多个问题。

第一个是,您的计数变量numbox保存在您的销毁脚本中,该脚本放在每个盒子上。
这意味着,每个盒子都在计算自己。

您必须集中这个。有多种方法可以做到这一点。
一种方法是将此变量声明为静态(
这不是最好的实践,而是起作用。

一个更好的方法是在您的播放器上拥有一个脚本,该脚本容纳此数字,并且每个盒子都搜索此脚本,并在被销毁时更改此数字。

第二个大问题是,您在更新中做了一些非常奇怪的事情,

首先是碰撞处理,您将 iSdestroyed 设置为true。然后,在每个帧中调用的框方法中,您将 numbox 变量降低一个,如果被销毁是正确的。

因此,如果您的盒子被击中,您将减少每个框架。

之后,您正在检查每个帧,如果您的numbox为119
如果是这样,您会更改场景。

这就是原因,为什么只有一个男孩

这个行为很奇怪,因为这是完全不必要的。您可以在OnCollisionEnter2D方法中直接减少变量。

有一些小事情,可以改进。

  1. 当您尝试播放声音时,您不必在代码中指定AudioClip。您可以通过拖放直接在AudioSource组件上的统一分配此内容。这使您的代码更加简单。
  2. 您没有打电话给两个Coroutine。您已经指定了此Coroutine,但不要称呼它。
    //Script on Player
    public class PlayerBoxDestroyManager:MonoBehaviour
    {
        public int StartBoxes = 120;
        private int Boxes;
    
        private void Start()
        {
            Boxes = StartBoxes;
        }
    
        public void DestroyBox()
        {
            //Reduce our Boxes count
            //This is equal to Boxes -= 1 
            //                 Boxes = Boxes -1
            Boxes--;
    
            // If we have less or zero Boxes left, we End call our EndGame methode
            if(Boxes <= 0)
            {
                EndGame();
            }
        }
    
        private void EndGame()
        {
            // We change the Scene to the mainMenu
            SceneManager.LoadScene("mainManu");
        }
    }
    ```
//Script on all Boxes
public class Box : MonoBehaviour
{
    public string Balltag = "ball";

    //Audio Source the Audio Clip has to be assigned in the Unity editor
    public AudioSource Coin;

    private void OnCollisionEnter2D(Collision2D collision)
    {
        //Check it colliding Object has the right Tag
        if(collision.transform.tag == Balltag)
        {
            //Get the reference to the Player Script
            PlayerBoxDestroyManager PBDM = FindObjectOfType<PlayerBoxDestroyManager>();
            //We can now access the Destroy Box Methode
            PBDM.DestroyBox();

            //Play the sound
            Coin.Play();

            //If we destroy our Object now, the Sound would also be deletet. 
            //We want to hear the sound, so we have to wait, till the sound is finished.
            StartCoroutine(WaitTillAudioIsFinished());
        }
    }

    IEnumerator WaitTillAudioIsFinished()
    {
        //we wait till the sound is finished
        while (Coin.isPlaying)
        {
            yield return null;
        }
        //if finished, we destroy the Gameobject
        Destroy(gameObject);
    }
}

I hope I helped you. If you have questions, feel free to ask. 
And sorry for my English:)

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 your numBox 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.

  1. When you are trying to play a Sound, you don't have to specify the AudioClip in code. You can assign this directly in Unity on the AudioSource Component via drag and drop. This makes your code simpler.
  2. You are not calling the Two Coroutine. You've specified this Coroutine but don't call it.
    //Script on Player
    public class PlayerBoxDestroyManager:MonoBehaviour
    {
        public int StartBoxes = 120;
        private int Boxes;
    
        private void Start()
        {
            Boxes = StartBoxes;
        }
    
        public void DestroyBox()
        {
            //Reduce our Boxes count
            //This is equal to Boxes -= 1 
            //                 Boxes = Boxes -1
            Boxes--;
    
            // If we have less or zero Boxes left, we End call our EndGame methode
            if(Boxes <= 0)
            {
                EndGame();
            }
        }
    
        private void EndGame()
        {
            // We change the Scene to the mainMenu
            SceneManager.LoadScene("mainManu");
        }
    }
    ```
//Script on all Boxes
public class Box : MonoBehaviour
{
    public string Balltag = "ball";

    //Audio Source the Audio Clip has to be assigned in the Unity editor
    public AudioSource Coin;

    private void OnCollisionEnter2D(Collision2D collision)
    {
        //Check it colliding Object has the right Tag
        if(collision.transform.tag == Balltag)
        {
            //Get the reference to the Player Script
            PlayerBoxDestroyManager PBDM = FindObjectOfType<PlayerBoxDestroyManager>();
            //We can now access the Destroy Box Methode
            PBDM.DestroyBox();

            //Play the sound
            Coin.Play();

            //If we destroy our Object now, the Sound would also be deletet. 
            //We want to hear the sound, so we have to wait, till the sound is finished.
            StartCoroutine(WaitTillAudioIsFinished());
        }
    }

    IEnumerator WaitTillAudioIsFinished()
    {
        //we wait till the sound is finished
        while (Coin.isPlaying)
        {
            yield return null;
        }
        //if finished, we destroy the Gameobject
        Destroy(gameObject);
    }
}

I hope I helped you. If you have questions, feel free to ask. 
And sorry for my English:)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文