I’m making a rhythm game. Because it’s test, when entering MainScene I scripted code to play the song one second later using coroutine and OnSceneLoaded function. And when the player dies, MenuScene is loaded. If I press the startbutton to load scene(1), the song is played delayed. No, actually it is played faster than first scene load.
I can figure out the difference between the first load and the second load so I can timing it directly after the second load, but I want a more fundamental solution.
As I tried everything to fixed this problem, I found some suspected parts.
- If I don’t use delay function, like coroutine or Invoke, this problem didn’t seem to appear.
- I used Debug.Log to check if time is delayed but the difference between OnSceneLoaded and PlayBgm functions at the first and second loads was the same.
- I tried LoadSceneAsync, but it didn’t work.
public class GameManager : MonoBehaviour
{
WaitForSeconds wait = new WaitForSeconds(1f);
void OnSceneLoaded(Scene scene, LoadSceneMode loadSceneMode)
{
if (SceneManager.GetActiveScene().buildIndex == 1)
{
StartCoroutine(PlayBgm());
}
}
IEnumerator PlayBgm()
{
yield return wait;
SoundManager.Instance.PlayBgm(true);
}
void OnEnable()
{
SceneManager.sceneLoaded += OnSceneLoaded;
}
}
This is some of the main codes related to the question I took out among my codes.
Do you have any solution?