Home > Software design >  How do I change the loop value in script with this type of sound manager? I thought a simple if stat
How do I change the loop value in script with this type of sound manager? I thought a simple if stat

Time:05-30

The code I am posting is just one of the ways I have tried. For whatever reason I just cannot get the script to access the loop value like I see done with other public variables. Only piece I can add is that the sound that is trying to have the value altered is created in an OnAwake() method in another script.

public static void PlaySound(Sound sound, Vector3 position)
{
    if (CanPlaySound(sound))
    {
        GameObject soundGameObject = new GameObject("Sound");
        soundGameObject.transform.position = position;
        AudioSource audioSource = soundGameObject.AddComponent<AudioSource>();
        audioSource.clip = GetAudioClip(sound);

        Debug.Log(soundGameObject.GetComponent<AudioSource>().clip);

        if (audioSource.clip.Equals("Level 1 Music"))
        {
            soundGameObject.GetComponent<AudioSource>().loop = true;
        }

        audioSource.Play();

        Object.Destroy(soundGameObject, audioSource.clip.length);
    }
}

CodePudding user response:

It's because of your condition. The equals option actually measures the clip by the clip, not the clip by the name of the clip. Try this option to fix the problem:

if (audioSource.clip.name.Equals("Level 1 Music"))
{
    audioSource.loop = true;
}
  • Related