简体   繁体   English

音频不会在特定场景中停止播放

[英]Audio not stop playing in a specific scene

I am coding a C# script in Unity.我正在 Unity 中编写 C# 脚本。 I code audio playing in multiple scenes and it doesn't stop playing in another scene I expected.我对在多个场景中播放的音频进行了编码,并且它不会在我期望的另一个场景中停止播放。

This is the scene I start playing the audio script BgScript.cs .这是我开始播放音频脚本BgScript.cs的场景。

This code is used for playing and stopping audio.此代码用于播放和停止音频。

BgScript.cs BgScript.cs

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class BgScript : MonoBehaviour
{
private static BgScript instance = null;
    public static BgScript Instance
    {
        get { return instance; }
    }
    void Awake()
    {
        if (instance != null && instance != this)
        {
            Destroy(this.gameObject);
            return;
        }
        else
        {
            instance = this;
        }
        DontDestroyOnLoad(this.gameObject);
    }
}

This is the image of where I place a script AudioPause.cs for audio stopping.这是我放置用于音频停止的脚本AudioPause.cs的图像。 enter image description here在此处输入图像描述

And this is my audio stopping code这是我的音频停止代码

AudioPause.cs .音频暂停.cs

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class AudioPause : MonoBehaviour
{
    void start() {
        BgScript.Instance.gameObject.GetComponent<AudioSource>().Stop();
    }
}

start() is lowercased in AudioPause.cs and thus the code never runs because it is never called. start() 在 AudioPause.cs 中是小写的,因此代码永远不会运行,因为它永远不会被调用。

void Start() { }

https://docs.unity3d.com/ScriptReference/MonoBehaviour.Start.html https://docs.unity3d.com/ScriptReference/MonoBehaviour.Start.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM