I’m working on a game that will play a sound when the player collects a crystal. I’ve tried using the PlayOneShot line to make it play only once but that doesn’t seem to work. The only time I get a sound is when it is in the update function, but that just keeps looping it. I’ve attached the code to this post for your convenience. Thanks in advance!
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class GameManager : MonoBehaviour
{
public GameObject player;
public GameObject gameOverMenu;
public GameObject levelCompleteMenu;
public GameObject crystal;
//The following three objects are for the sound I want to play
public GameObject collectable;
public AudioClip coinCollect;
public AudioSource collectableSound;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
if(player == null)
{
//Restart();
gameOverMenu.SetActive(true);
}
if(crystal == null)
{
levelCompleteMenu.SetActive(true);
}
}
void Restart()
{
SceneManager.LoadScene("LevelOne");
}
//This function is for the sound that I am trying to play
void Collectable()
{
if(collectable == null)
{
collectableSound.PlayOneShot(coinCollect);
}
}
}