I want loop music. How can i make that?

71 views Asked by At

So, i want play music all time. when the scene is reloaded, the music starts from the beginning, but I want it to continue from the same moment using UnityEngine;

public class MusicBack : MonoBehaviour
{
private void Awake()
{
        GameObject[] objs = GameObject.FindGameObjectsWithTag("Music");

        if (objs.Length > 1)
        {
            Destroy(gameObject);
        }

        DontDestroyOnLoad(gameObject);
        Debug.Log("correct");
}
}
2

There are 2 answers

0
Sigmond Kukla On

I think that you'll need a custom script, like suggested here: https://answers.unity.com/questions/1260393/make-music-continue-playing-through-scenes.html

The idea with that answer is to use DontDestroyOnLoad to keep the audio source the same throughout scenes. So if you move from Scene 1 to Scene 2, the audiosource is still Scene 1's, if I interpreted taht correctly. Then there's just some logic for not playing the audio in scenes where it shouldn't be played.

Probably this could have been found with a quick google search though.

0
KiynL On

Enable the loop option in AudioSource. If the music plays in the whole scene, 2D it as well.

enter image description here