Question by
Dagisdown · Sep 28, 2020 at 10:52 AM ·
soundsoundstimer countdownsound effectstimer-script
How can i make countdown tick-tock sound ?
Hi, I am making a tower defense game. And i want to make tick-tock countdown sound but when i call "play sound" function in the code, it just keeps infinitely looping. i also used coroutine yet it didn't work. Please Help me.
==================================================================
Wave Spawner
public float timeBetweenWaves = 34f;
private float countdown = 34f;
void Update()
{
if (waveNumber <= 40)
{
SpawnCount();
}
}
void SpawnCount()
{
GameObject[] enemyFind = GameObject.FindGameObjectsWithTag("Enemy");
int enemyCount = enemyFind.Length;
if (enemyCount == 0) // if enemy is 0, countdown goes 34 to 0
{
if (countdown <= 0f) // if countdown becomes 0, wave starts and countdown stops at 34
{
StartCoroutine(SpawnWave()); // wave starts here
countdown = timeBetweenWaves; // countdown initializes
}
countdown -= Time.deltaTime; // countdown goes 34 to 0
waveCountDownText.text = Mathf.Round(countdown).ToString();
// I want to play countdown tick-tock sound here until countdown becomes 0
//FindObjectOfType<SoundManager>().Play("Timer"); // this didn't work
}
}
Sound Manager
public Sound[] sounds;
public static SoundManager instance;
void Awake()
{
if (instance == null)
{
instance = this;
}
else
{
Destroy(gameObject);
return;
}
DontDestroyOnLoad(gameObject);
foreach (Sound s in sounds)
{
s.source = gameObject.AddComponent<AudioSource>();
s.source.clip = s.clip;
s.source.volume = s.volume;
s.source.pitch = s.pitch;
s.source.loop = s.loop;
}
}
void Start()
{
Play("Theme");
}
public void Play (string name) // i used this function and sound name is "Timer"
{
Sound s = Array.Find(sounds, sound => sound.name == name);
if (s == null)
{
Debug.LogWarning("Sound: " + name + "Not Found!");
return;
}
s.source.Play();
}
Comment