My rocket keeps crashing at the launching pad when the next level is loaded. Console tells me this:
NullReferenceException: Object reference not set to an instance of an object
CollisionHandler.StartCrashSequence () (at Assets/Script/CollisionHandler.cs:58)
By the way, can you teach me how to properly copy and paste my codes here? My posting seems not working like other people’s:
using System;
using UnityEngine;
using UnityEngine.SceneManagement;
public class CollisionHandler : MonoBehaviour
{
[SerializeField] float levelLoadDelay = 2f;
[SerializeField] AudioClip success;
[SerializeField] AudioClip crash;
[SerializeField] ParticleSystem successParticles;
[SerializeField] ParticleSystem crashParticles;
AudioSource audioSource;
//This is the 1st time I see a State used.
bool isTransitioning = false;
void Start()
{
audioSource = GetComponent<AudioSource>();
}
private void OnCollisionEnter(Collision other)
{
if(isTransitioning){return;}
switch (other.gameObject.tag)
{
case "Friendly":
Debug.Log("This thing is friendly");
break;
case "Finish":
StartSuccessSequence();
break;
case "Fuel":
Debug.Log("You picked up fuel");
break;
default:
StartCrashSequence();
break;
}
}
void StartSuccessSequence()
{
isTransitioning = true;
audioSource.PlayOneShot(success);
successParticles.Play();
GetComponent<Movement>().enabled = false;
Invoke("LoadNextLevel", levelLoadDelay);
}
void StartCrashSequence()
{
isTransitioning = true;
audioSource.PlayOneShot(crash);
crashParticles.Play();
// to add particle effect upon crash
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);
}
}