0

I have a problem, after one audio I immediately have another, how do I make the audio play every 30 seconds. Thanks!!! Code. I try to use WaitForSeconds(6) but it wait 6 sec only before first traack

using UnityEngine;
using System.Collections;

public class RandomAudio : MonoBehaviour
{
    public AudioClip[] soundtrack;

    // Use this for initialization
    void Start()
    {
        if (!GetComponent<AudioSource>().playOnAwake)
        {
            GetComponent<AudioSource>().clip = soundtrack[Random.Range(0, soundtrack.Length)];
            GetComponent<AudioSource>().Play();
        }
    }

    // Update is called once per frame
    void Update()
    {
        if (!GetComponent<AudioSource>().isPlaying)
        {
            GetComponent<AudioSource>().clip = soundtrack[Random.Range(0, soundtrack.Length)];
            GetComponent<AudioSource>().Play();
        }
    }
}
Sviatoslav
  • 27
  • 2
  • make your Update wat for 30 seconds? – derHugo May 20 '21 at 15:07
  • You are starting your audio source on `Start()` (which is called on initialization) AND every `Update()` to check wether it is playing or not. You might be referencing the same audio clip as 2 different objects in your editor, therefore your `Start()` method plays the audio clip and so does your code in the `Update()` method. I would suggest you delete your `Update()`code block. The solution might depend on your specific Game/Scene use but nonetheless, you are are calling your audio component twice. – SecureCake May 21 '21 at 01:29

0 Answers0