Hi, I'm facing a very specific problem, so this question will have long and detailed explanations about the circumstances. If that's not your style, I don't suggest reading this, and it's okay, I can't focus on long paragraphs too. You've been warned.
So I'm trying to play a sound when the player starts the game. There are two ways the player can start: when they press space (or whatever is in `input.GetButtonDown("Submit")`) or when they click the play button. In both cases the `play()` function is called in a separate gameobject I call "musicPlayer". The function:
public void play()
{
sound.PlayOneShot(playSound);
SceneManager.LoadScene(1, LoadSceneMode.Single);
}
That gameobject is responsible for the scene logic and the sound playing, the only difference is that I check the space press from the musicPlayer
void Update()
{
if (Input.GetButtonDown("Submit") && SceneManager.GetActiveScene().buildIndex == 0)
{
play();
}
}
and the the `play()` function gets called from outside the musicPlayer:
![alt text][1]
Now it's important to know that this music player is a prefab. I have this in every scene, and because it plays the music, it's `DontDestroyOnLoad`. And in order to avoid stacking, I destroy it on start, if there's already one with this code:
void Start()
{
int musics = FindObjectsOfType().Length;
if (musics > 1)
{
Destroy(gameObject);
}
else
{
DontDestroyOnLoad(gameObject);
}
}
Because of that, the buttons refer to directly the prefab in the files, this way it calls the function, no matter which scene was the starting one. But when I call `play()` from the button, it doesn't play the sound, and I get this error:
![alt text][2]
However I checked that it's not disabled. The gameobject "musicPlayer" is definitely not destroyed, and the strangest this is that the function gets called. The game starts, no matter which way it's started, but only when I call `play()` from the button, the sound doesn't get played. What's even stranger is the fact that if I switch to the musicPlayer in the scene instead of the prefab in the button, the music plays, but I can't do that because that way it doesn't work if another scene is the first one.
So to summarize it: there is a gameobject called musicPlayer. It's a prefab, and that untouched prefab is in every scene. It plays music, so it's `DontDestroyOnLoad`, and I destroy it on start, if there's already one in the scene. There's a function in it `play()`. That function plays a sound and starts the game. It is called when I press space in the menu scene, or when I press a button which only exists in that scene. If I press space, everything works, but when i press the button, the sound doesn't play, but the scene starts. The musicPlayer isn't destroyed, because it's `DontDestroyOnLoad`, the `play()` function gets correctly called, because it starts the scene, and the audio source is enabled. How can I make it play the sound? Thank you for taking your time to read this.
[1]: /storage/temp/203404-kep-2022-12-30-114148688.png
[2]: /storage/temp/203405-kep-2022-12-30-115833507.png
↧