Public function call not working

I’m trying to call the Game Over scene after a short delay, and I keep getting stuck on the same error… Object reference not set to an instance of an object… GameSession.cs:14. I’ve wasted too much time trying to sort this out and used the same code elsewhere and had it work fine. Calling GameOver() from another script the same way works fine haha. What am I missing?

using System.Collections;
using UnityEngine;

public class GameSession : MonoBehaviour 
{
    // Config Parameters
    [SerializeField] float gameOverDelay = 2.5f;      // Length of time to wait before displaying Game Over scene

    public IEnumerator GameOver()
    {
        yield return new WaitForSeconds(gameOverDelay);

        SceneSwitch sceneSwitch = FindObjectOfType<SceneSwitch>();
        sceneSwitch.LoadGameOver();        // ******** Line 14 ************
    }
}
using UnityEngine;
using UnityEngine.SceneManagement;

public class SceneSwitch : MonoBehaviour 
{
    public void LoadGameOver()
    {
        SceneManager.LoadScene(2);
    }
}

Solved it… I didn’t have SceneSwitch attached to a game object, it was a component of nothing.

1 Like

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms