- Home /
Unity: Attaching a volume control slider that doesn't always exist, to another object.
Hello everyone, I am really new to Unity and have been trying to create a small game. In this game there is an options menu, which contains two sliders, one for the background music volume, and one for the SFX volume. I created the options menu within it's own scene, and have a persistent empty object which plays the game's music. The music player uses this simple C# script:
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class musicVolume : MonoBehaviour
{
public Slider musicControlSlider;
public AudioSource backgroundMusic;
void Start()
{
backgroundMusic.Play();
}
void Update()
{
{
backgroundMusic.volume = musicControlSlider.value;
}
}
}
While this does successfully keep the music playing at whatever the user set in the options menu with the slider, I noticed a couple serious issues, and have been unable to resolve them.
Firstly, when I change scenes the music control slider is set to missing, as one would expect, however if I return to the options menu, the slider becomes set to "None" rather than what I graphically set it to (MusicSlider (Slider)).
Secondly, I am unable to graphically reset the slider while play mode is enabled in the editor.
Is there any way to detect the musicslider (MusicSlider), which is a child of optionsMenu, Canvas, optionsManager in the scene options whenever musicprefab (the background music object) enters the scene? That is I want this to happen whenever musicprefab enters the scene
Rather than this
Thank you.