After some experimenting I’ve stumbled on a minor bug that’s confusing me. I’ve no idea why it’s an issue, but basically, each level has a Screen Fader object in the Hierarchy. It’s essentially a canvas, which has my Screen Fader script on and has a Black Screen child object. When each scene loads, a coroutine is called and fades the screen to black for a second and then unfades. If I deactivate one of them in a level via the Inspector, pause the game, select Restart (restarts the entire game), the screen remains black and has an error in the console saying ‘object reference not set to an instance of an object’. Clicking on this highlights line 49 in my PauseMenu script, which is where it tries to call the Screen Fader’s coroutine. I don’t understand why this is a problem. As I say, each scene has its own Screen Fader object set up, the black screen is ‘on’ by default (apart from one scene), so surely once the level is destroyed, that coroutine will be called from the first level’s own Screen Fader…?
These are the two scripts:
PauseMenu:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class PauseMenu : MonoBehaviour
{
public static bool GamePaused;
public GameObject pauseMenuUI;
private ScreenFader theScreenFader;
void Start()
{
theScreenFader = FindObjectOfType<ScreenFader>();
ControllerDetection();
GamePaused = false;
}
void Update()
{
if (Input.GetKeyDown(KeyCode.Escape) || Input.GetKeyDown("joystick button 7") || Input.GetKeyDown("joystick button 9"))
{
if (GamePaused)
{
Resume();
}
else
{
Pause();
}
}
}
public void Resume()
{
pauseMenuUI.SetActive(false);
Time.timeScale = 1f;
GamePaused = false;
//theScreenFader.blackScreen.enabled = true;
PlayerController.canMove = true;
}
public void Restart()
{
SceneManager.LoadScene("start_area");
Debug.Log("level loaded");
pauseMenuUI.SetActive(false);
Debug.Log("pause menu disabled");
ScreenFader.black = true;
Debug.Log("screen is black");
theScreenFader.StartCoroutine("ScreenFade");
Time.timeScale = 1f;
}
public void Pause()
{
Cursor.visible = true;
pauseMenuUI.SetActive(true);
Time.timeScale = 0f;
PlayerController.canMove = false;
GamePaused = true;
//theScreenFader.blackScreen.enabled = false;
}
public void QuitGame()
{
Application.Quit();
}
public void ControllerDetection()
{
string[] names = Input.GetJoystickNames();
for (int x = 0; x < names.Length; x++)
{
//print(names[x].Length);
if (names[x].Length == 19)
{
//print("PS4 CONTROLLER IS CONNECTED");
SceneManagement.ps4Controller = 1;
SceneManagement.xbox360Controller = 0;
if (SceneManagement.ps4Controller == 1)
{
//Debug.Log("PS4 controller detected");
}
}
else if (names[x].Length == 33)
{
//print("XBOX 360 CONTROLLER IS CONNECTED");
SceneManagement.ps4Controller = 0;
SceneManagement.xbox360Controller = 1;
if (SceneManagement.xbox360Controller == 1)
{
//Debug.Log("Xbox 360 controller detected");
}
}
else
{
SceneManagement.ps4Controller = 0;
SceneManagement.xbox360Controller = 0;
}
if (SceneManagement.xbox360Controller == 0 && SceneManagement.ps4Controller == 0)
{
//Debug.Log("No controllers detected");
}
}
}
}
ScreenFader:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
public class ScreenFader : MonoBehaviour
{
public Image blackScreen;
public float fadeSpeed;
public float waitForFade;
//public static ScreenFader instance;
public static bool black;
private bool isFadeToBlack;
private bool isFadeFromBlack;
//private bool isBlack;
/*void Awake()
{
if (instance != null)
{
Destroy(gameObject);
}
else
{
instance = this;
DontDestroyOnLoad(gameObject);
}
}*/
void Start()
{
StartCoroutine("ScreenFade");
}
IEnumerator ScreenFade()
{
if (SceneManager.GetActiveScene() == SceneManager.GetSceneByName("level 1, room 1"))
{
isFadeToBlack = false;
}
else if (black)
{
Debug.Log("black");
yield return new WaitForSeconds(fadeSpeed);
isFadeToBlack = true;
yield return new WaitForSeconds(waitForFade);
isFadeFromBlack = true;
yield return new WaitForSeconds(2f);
}
else
{
yield return new WaitForSeconds(fadeSpeed);
isFadeToBlack = true;
yield return new WaitForSeconds(waitForFade);
isFadeFromBlack = true;
yield return new WaitForSeconds(2f);
}
}
void Update()
{
if (isFadeToBlack)
{
blackScreen.color = new Color(blackScreen.color.r, blackScreen.color.g, blackScreen.color.b, Mathf.MoveTowards(blackScreen.color.a, 1f, fadeSpeed * Time.deltaTime));
if (blackScreen.color.a == 1f)
{
isFadeToBlack = false;
}
}
if (isFadeFromBlack)
{
blackScreen.color = new Color(blackScreen.color.r, blackScreen.color.g, blackScreen.color.b, Mathf.MoveTowards(blackScreen.color.a, 0f, fadeSpeed * Time.deltaTime));
if (blackScreen.color.a == 0f)
{
isFadeFromBlack = false;
}
}
if (black)
{
blackScreen.color = new Color(blackScreen.color.r, blackScreen.color.g, blackScreen.color.b);
if (blackScreen.color.a == 1f)
{
black = false;
}
}
}
}