I recently made a script where when a player place a fuse in a fusebox, the generator will be active. When said thing happen, the generator will play a looped sound.
Instead however, the generator do play the sound but it created unlimited AudioOneShots which anyone can guess, is bad. Below are the coding.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Generator : MonoBehaviour {
public bool GeneratorOn;
public AudioClip GeneratorOnSound;
// Use this for initialization
void Start ()
{
GeneratorOn = false;
}
// Update is called once per frame
void Update ()
{
if (Fusebox.FuseOn == true)
{
GeneratorOn = true;
AudioSource.PlayClipAtPoint (GeneratorOnSound, transform.position);
GetComponent<AudioSource>().clip = GeneratorOnSound;
GetComponent<AudioSource>().Play();
}
//if (GeneratorOn == true)
//{
//AudioSource.PlayClipAtPoint (GeneratorOnSound, transform.position);
//GetComponent<AudioSource>().clip = GeneratorOnSound;
//GetComponent<AudioSource>().Play();
//}
}
}
Any help will be very appreciated.