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;
}
}
Please read the [user guide][1] > We have created a new section called > the Help Room [...]. The Help Room > will also contain posts where users > are asking for more general help with > problem solving. We have tons of question about NullReference exceptions. They are considered simple debugging questions since a NullReference exception is always the same problem. I'll move your question to the help room. [1]: http://answers.unity3d.com/page/userguide.html
– Bunny83The error message is reporting a problem at line 261 of PausePanel.cs. You've only posted 80 lines....
– tanoshimi