I have created 2 scripts: a music transition script and a musicdisruptor script
The music transition script allows for the audio source component to be loaded into the next scene causing a smooth musical transition that doesntbreak or restart. However I want ti to stop passing over once in a specific scene, so I created the musicDisruptor script:
public string sceneName;
private void Start()
{
}
void Interrupt()
{
//Check to see what scene I'm currently at if matches the name destroy the script on component
if (SceneManager.GetActiveScene().name == sceneName)
{
Destroy(GetComponent<MusicTransition>());
}
}
But is doesn’t work! Am I referingto it wrong? Also, I’m a week C# programmer who’s been experimenting on my own codes recently.
Thanks for all the assistance in advance!
I’m sorry about that… the functio would be called in the music transition script
It went something like this(I changed it to try something else and don’t remember the sytanx)
I know it’s wrong this time cause the editor said so, but it was something like that. I’ve also fully realised that it’s being called to next scene and being protected by the DontDestroyOnLoad function which got me thinking, could I stop that instance for my specified scene to work?
You no have one script that transfers a script from scene to scene and another that detects whether it’s in the wrong scene and destroys it. Maybe you could just not transfer it if you don’t to to keep the logic in one script.
You are currently working with destroy. There are more subtle ways to silence audio. You could also always transfer it and then turn down the volume for a specific scene. Or stop/pause the audio.
I have given it that thought, but like I said. I’m still a greeny to programming specifically for games and grasping how to use C# better on each project
When I think of how I’d do that I think of animating the sound to fall off over time and keep the game object, which now that I think of it, I could’ve! Thank you soo much for your help @jvo3dc your input has really helped me to find a solution and I have figured out a different way to subjugate the DontDestrOnLoad, I did it in one script like you said I could, I did it on the MusicTransition script, here it is!:
public class MusicTransition : MonoBehaviour
{
private static MusicTransition instance;
public string stopAtScene;
void Awake()
{
if (instance == null)
{
instance = this;
DontDestroyOnLoad(instance);
SceneManager.sceneLoaded += OnSceneLoaded;
}
else
{
Destroy(gameObject);
}
}
void OnSceneLoaded(UnityEngine.SceneManagement.Scene scene, UnityEngine.SceneManagement.LoadSceneMode mode)
{
if (scene.name == stopAtScene)
{
Destroy(gameObject);
Debug.Log("The music stopped!");
}
}
}
Once again, thank you for your help, couldn’t have found the solution as yet if not for your input, I will try the second method you suggested on fading/ducking out the audio tho;)