[Fixed] NullReferenceException Error help

I’ve checked my code out with ricks and mines the same, but everytime i try to playtest my game the NullReferenceExeption error shows up. My rocket doesnt crash or go to the next level, heres my code:

using UnityEngine;
using UnityEngine.SceneManagement;
public class CollisionHandler : MonoBehaviour
{
[SerializeField] float Delay = 1;
[SerializeField] AudioClip success;
[SerializeField] AudioClip crash;
AudioSource audioSource;
void Start()
{
audioSource.GetComponent();
}
void OnCollisionEnter(Collision other)
{
switch (other.gameObject.tag)
{
case “Friendly”:
Debug.Log(“good”);
break;
case “Finish”:
SuccessSequence();
break;
default:
StartCrashSequence();
break;
}
}
void SuccessSequence()
{
//todo add victory partical effect
audioSource.PlayOneShot(success);
Invoke(“LoadNextScene”, Delay);
GetComponent().enabled = false;
}
void StartCrashSequence()
{
audioSource.PlayOneShot(crash);
// todo add partical effects upon crash
GetComponent().enabled = false;
Invoke(“ReloadLevel”, Delay);
}
void ReloadLevel()
{
int currentSceneIndex = SceneManager.GetActiveScene().buildIndex;
SceneManager.LoadScene(currentSceneIndex);
}
void LoadNextScene()
{
int currentSceneIndex = SceneManager.GetActiveScene().buildIndex;
int NextSceneIndex = currentSceneIndex + 1;
if (NextSceneIndex == SceneManager.sceneCountInBuildSettings)
{
NextSceneIndex = 0;
}
SceneManager.LoadScene(NextSceneIndex);
}
}
fix: had to update prefab

1 Like

Great job with your code.

On your start code you have

“audioSource.GetComponent();”

and you should put

audioSource = GetComponent< AudioSource >();

Hope this helps!

1 Like

Privacy & Terms