I created a music Randomizer script and for some reason it’s causing a noticeable amount of lag. It’s pretty small so I’m not sure why it’s causing lag. Though I’m pretty sure the script is causing the lag. The lag occurs immediately after I attach the script to a gameobject and when I remove it from the gameobject the lag is gone. Any ideas as to why this is occurring?
using UnityEngine;
public class Music : MonoBehaviour {
public AudioSource audioSource;
public AudioClip[] sounds;
public AudioClip sound;
public float wait;
void Start(){
audioSource = GetComponent<AudioSource>();
sounds = Resources.LoadAll<AudioClip>("Music");
}
void Update(){
if(wait <= 0f){
sound = sounds[Random.Range(0, 10)];
audioSource.clip = sound;
audioSource.Play();
wait=sound.length;
}
else if(wait > 0f){
wait -= Time.deltaTime;
}
}
}