Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
174 views
in Technique[技术] by (71.8m points)

c# - Audio not stop playing in a specific scene

I am coding a C# script in Unity. 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.

This code is used for playing and stopping audio.

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. enter image description here

And this is my audio stopping code

AudioPause.cs.

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

public class AudioPause : MonoBehaviour
{
    void start() {
        BgScript.Instance.gameObject.GetComponent<AudioSource>().Stop();
    }
}
question from:https://stackoverflow.com/questions/65874006/audio-not-stop-playing-in-a-specific-scene

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

start() is lowercased in AudioPause.cs and thus the code never runs because it is never called.

void Start() { }

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...