so i am making a game and in that game player can change background music by clicking different buttons i did the script attached the script and everything was working fine audioclips were changing in audiosource on button clicks but audio wasn’t playing then i checked my audiosourcemanager everything ewas fine it wasn’t mute or disabled but the music wasn’t playing then i disabled the audiosource component and enabled it again and it worked but when i changed music audioclip was replaced but same thing happend i tried to disable and enable the componnet via script it didn’t i also tried to disable the whole gameobject and enable it again but it didn’t work either here’s the script ,so i am making a game and player can change music in the game i created a script and everything was working fine the musics were changing on button clicks but the audio was not playing i checked my audiosourcemanager and everything was fine but music wasn’t playing and then i disabled the audiosource component and reenable it and the music started playing but when i change the music same thing happened i tried to deactive and enable it again from the script but it wasn’t work from script here’s the script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class musicm : MonoBehaviour
{
public static musicm instance;
public AudioSource musicdefault;
public GameObject bgPrefab;
public bool changed = false;
public AudioClip struggle;
public static musicm spriteinstance;
bool isChanged;
public AudioClip lostkingdom;
public AudioClip aspire;
private void Awake()
{
if (spriteinstance == null)
{
spriteinstance = this;
}
else
{
Object.Destroy(gameObject);
}
musicdefault.Play();
}
private void Start()
{
}
private void Update()
{
if (PlayerPrefs.GetInt("music") == 1)
{
musicdefault.clip = struggle;
}
if (PlayerPrefs.GetInt("music") == 2)
{
musicdefault.clip = lostkingdom;
}
if (PlayerPrefs.GetInt("music") == 3)
{
musicdefault.clip = aspire;
}
}
public void struggle1()
{
PlayerPrefs.SetInt("music", 1);
musicdefault.clip = struggle;
changed = true;
}
public void lostkingdom1()
{
PlayerPrefs.SetInt("music", 2);
musicdefault.clip = lostkingdom;
changed = true;
musicdefault.enabled = true;
}
public void aspire1()
{
PlayerPrefs.SetInt("music", 3);
musicdefault.clip = aspire;
changed = true;
}
}