I can’t seem to get the Die and Win audioclips to work. The console gives no error.
I’ve put the audioclips in the serialized fields and i feel like i’ve triple checked the code in the lecture now.
I also saw a different post where the audioclips from the movement.cs file had to be called on outside of the update() function but i already have that.
Could this be a version issue? i’m working in 2019.4.18f1.
regardless thanks in advance for anyone able to help
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class CollisionHandler : MonoBehaviour
{
[SerializeField] float gracePeriod = 1f;
[SerializeField] AudioClip Bonk;
[SerializeField] AudioClip Winnar;
AudioSource audioSource;
void Start()
{
audioSource = GetComponent<AudioSource>();
}
void OnCollisionEnter(Collision collision)
{
switch (collision.gameObject.tag)
{
case "Friendly":
Debug.Log("You hit a friendly object");
break;
case "Finish":
FinishLevel();
break;
default:
StartCrashSequence();
break;
}
}
private void FinishLevel()
{
//todo add sfx on finish level
audioSource.PlayOneShot(Winnar);
//todo add vfx on finish level
StopMovement();
Invoke("LoadNextLevel", gracePeriod);
}
void StartCrashSequence()
{
//todo add sfx on crash
audioSource.PlayOneShot(Bonk);
//todo add vfx on crash
StopMovement();
Invoke("ReloadLevel", gracePeriod);
}
void ReloadLevel()
{
int currentSceneIndex = SceneManager.GetActiveScene().buildIndex;
SceneManager.LoadScene(currentSceneIndex);
}
void LoadNextLevel()
{
int currentSceneIndex = SceneManager.GetActiveScene().buildIndex;
int nextSceneIndex = currentSceneIndex + 1;
if (nextSceneIndex == SceneManager.sceneCountInBuildSettings)
{
nextSceneIndex = 0;
}
SceneManager.LoadScene(nextSceneIndex);
}
void StopMovement()
{
GetComponent<Movement>().enabled = false;
GetComponent<AudioSource>().Stop();
}
}