(Complete unity course - Project Boost - Bool Variable For State) I’ve been like 1h with this problem,i am at the step where you implement the “audioSource.Stop” step to stop the engine sound playing,and no sound plays after i do it,also the “istransitioning {return}” didn’t work until i’ve put it outside the switch,here is my code and thanks for the help
{
using UnityEngine;
using UnityEngine.SceneManagement;
public class CollisionHandler : MonoBehaviour
{
[SerializeField] float levelLoadDelay = 2f;
[SerializeField] AudioClip explosion;
[SerializeField] AudioClip success;
AudioSource audioSource;
bool isTransitioning = false;
void Start()
{
audioSource = GetComponent<AudioSource>();
}
void OnCollisionEnter(Collision other)
{
if (isTransitioning) { return; }
switch (other.gameObject.tag)
{
case "Friendly":
Debug.Log("This thing is friendly");
break;
case "Finish":
StartSuccessSequence();
break;
default:
StartCrashSequence();
break;
}
}
void StartSuccessSequence()
{
isTransitioning = true;
audioSource.Stop();
audioSource.PlayOneShot(success);
GetComponent<Movement>().enabled = false;
Invoke("LoadNextLevel", levelLoadDelay);
}
void StartCrashSequence()
{
isTransitioning = true;
audioSource.Stop();
audioSource.PlayOneShot(explosion);
GetComponent<Movement>().enabled = false;
Invoke("ReloadLevel", levelLoadDelay);
}
void LoadNextLevel()
{
int CurrentSceneIndex = SceneManager.GetActiveScene().buildIndex;
int nextSceneIndex = CurrentSceneIndex + 1;
if (nextSceneIndex == SceneManager.sceneCountInBuildSettings)
{
nextSceneIndex = 0;
}
SceneManager.LoadScene(nextSceneIndex);
}
void ReloadLevel()
{
int CurrentSceneIndex = SceneManager.GetActiveScene().buildIndex;
SceneManager.LoadScene(CurrentSceneIndex);
}
}}