- Home /
Question by
CookieHCl · Aug 20, 2019 at 01:59 AM ·
audioscenesplayoneshot
How do I PlayOneShot for multiple scenes?
I've made a singleton Object with AudioSource, and make it DontDestroyOnLoad so it can stay with multiple scenes.
Singleton worked perfectly, but sound breaks when I change scenes.
I've found that sound doesn't break when you use Play, but I have to use PlayOneShot so I don't have to make a lot of AudioSources.
Is there a way to make PlayOneShot doesn't break through multiple scenes?
Singleton Code
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SEController : MonoBehaviour
{
static SEController _instance;
public static SEController Instance
{
get
{
if (!_instance)
{
_container = new GameObject();
_container.name = "SEController";
_instance = _container.AddComponent<SEController>();
m_AudioSource = _container.AddComponent<AudioSource>();
m_AudioSource.playOnAwake = false;
DontDestroyOnLoad(_container);
}
return _instance;
}
}
static GameObject _container;
static AudioSource m_AudioSource;
public void PlayOneShot(AudioClip audioClip)
{
m_AudioSource.PlayOneShot(audioClip);
}
}
Comment