Invoke issue SOLVED

Ok so got the error where ReloadLevel scene is not called, and other errors when trying to fix the Invoke issue.
To solve this issue just move your ReloadLevel method below the last curly brace for OnCollisionEnter(Collision other) method.
Same for next scene(this will help later)
My script below my method names might be different from yours so you might need to change the names…:

using UnityEngine.SceneManagement;

using UnityEngine;

using System;

public class collisionHandler : MonoBehaviour

{

[SerializeField] AudioClip Success;

[SerializeField] AudioClip DeathExp;

AudioSource audioSource;

void Start()

{

   audioSource = GetComponent<AudioSource>();

}

void OnCollisionEnter(Collision other)

{



    switch (other.gameObject.tag)

    {

        case "friendly":

            friendly();

        break;

        case  "Finish":

            finish();

        break;

        default:

          audioSource.PlayOneShot(DeathExp);

          CrashSeq();    

                     

        break;

    }

    void CrashSeq()

    {

     GetComponent<Movement>().enabled = false;

     

     Invoke("ReloadLevel", 2f);

    }

    void friendly()

        {

            Debug.Log("I am friendly");

        }

    void finish()

        {

            Debug.Log("Yeah you made it");

            audioSource.PlayOneShot(Success);

        Invoke("nextScene", 2f);

           

        }

}

void ReloadLevel()

        {

            Debug.Log("Sorry you crashed and burned!!");

            int currentSceneIndex = SceneManager.GetActiveScene().buildIndex;

            SceneManager.LoadScene(currentSceneIndex);

        }

void nextScene()

{

int currentSceneIndex = SceneManager.GetActiveScene().buildIndex;

            int nextSceneIndex = currentSceneIndex +1;

            if (nextSceneIndex == SceneManager.sceneCountInBuildSettings)

            {

                nextSceneIndex = 0;

            }

            SceneManager.LoadScene(nextSceneIndex);



}

}

1 Like

Privacy & Terms