Coroutine Issue

I have been trying to debug and cannot figure out this issue:

NullReferenceException: Object reference not set to an instance of an object
PausePanel+c__Iterator1.MoveNext () (at Assets/Scripts/Background Scripts/PausePanel.cs:261)
UnityEngine.MonoBehaviour:StartCoroutine(IEnumerator)
PausePanel:YESQUIT() (at Assets/Scripts/Background Scripts/PausePanel.cs:151)
UnityEngine.EventSystems.EventSystem:Update()

This is the line of code with the issue: float fadeTime = GameObject.Find (“Scene Fader”).GetComponent ().BeginFade (1);

I have checked the spelling of my gameObject and it is correct and the the Scene Fader gameobject has the SceneFader script attached to it.

I have buttons that transition between levels - Alot of the tutorials assume you have your level system set up progressively so I’m having trouble adjusting to my setup.

Here’s my code:

SceneFader:

using UnityEngine;
using System.Collections;
using UnityEngine.SceneManagement;

public class SceneFader : MonoBehaviour {

	public Texture2D fadeOutTexture;
	public float fadeSpeed = 0.8f;

	private int drawDepth = -1000;
	private float alpha = 1.0f;
	private int fadeDir = -1;


	void OnGUI(){

		alpha += fadeDir * fadeSpeed * Time.deltaTime;
		alpha = Mathf.Clamp01 (alpha);
		GUI.color = new Color (GUI.color.r, GUI.color.g, GUI.color.b, alpha);
		GUI.depth = drawDepth;
		GUI.DrawTexture(new Rect (0,0,Screen.width ,Screen.height),fadeOutTexture);

	}

	public float BeginFade(int direction){

		fadeDir = direction;
		return (fadeSpeed);

	}

	void OnLevelWasLoaded(){

		BeginFade (-1);

	}
}

PausePanel:

using System.Collections;
using UnityEngine.SceneManagement;
using UnityEngine.UI;

public class PausePanel : MonoBehaviour {

public static bool isPaused=false;

public GameObject pauseMenuCanvas;

void Start(){

}

// Update is called once per frame
void Update () {
	
	if (isPaused == true) {

		pauseMenuCanvas.SetActive (true);
		Time.timeScale = 0f;

	} else if (isPaused==false && TimeChangingPowerUp.Active == true) {

		pauseMenuCanvas.SetActive(false);
		Time.timeScale = 0.5f;

	}

	else {

		pauseMenuCanvas.SetActive(false);
		Time.timeScale = 1f;

	}
		
}

public void Restart(){

	AreYouSureYouWantToRestart.SetActive (true);

}

public void YESRESTART(){

	//SceneManager.LoadScene ("Gameplay");
	//SceneFader.instance.LoadLevel("Gameplay");
	StartCoroutine (SceneFaderGamePlay());

}

public void YESQUIT(){

	//SceneFader.instance.LoadLevel("Title Menu");
	//SceneManager.LoadScene ("Title Menu");
	StartCoroutine (SceneFaderHome());

}

public void Pause(){

	isPaused = true;
	pauseMenuCanvas.SetActive(true);
	Time.timeScale = 0f;

}

IEnumerator SceneFaderGamePlay(){

	float fadeTime = GameObject.Find ("Scene Fader").GetComponent<SceneFader> ().BeginFade (1);
	yield return new WaitForSeconds (fadeTime);
	SceneManager.LoadScene ("Gameplay");
	isPaused = false;

}

IEnumerator SceneFaderHome(){

	float fadeTime = GameObject.Find ("Scene Fader").GetComponent<SceneFader> ().BeginFade (1);
	yield return new WaitForSeconds (fadeTime);
	SceneManager.LoadScene ("Title Menu");
	isPaused = false;

}

}

Inside your coroutine you only have one line that actually involves references which could be null and that is:

float fadeTime = GameObject.Find ("Scene Fader").GetComponent<SceneFader> ().BeginFade (1);

The possible reasons are:

  • There is no gameobject called “Scene Fader” and thus GameObject.Find returns “null”.
  • If there is a gameobject with that name the next thing is that it doesn’t have a SceneFader script attached and GetComponent returns “null”.

If a reference is “null” you can’t use it for anything. So in the first case when there is no gameobject the GetComponent call will cause a Null-Ref-Exception. In the second case, when there is a gameobject with that name but GetComponent returns null the call to BeginFade will cause a Null-Ref-Exception.