- Home /
Other
Scene Fader - Please Help!
This is the first time I have had this issue and I have been working on this project for 6 months and am almost done. I have seen several others with this issue, but no one has answered. Below you will find my code as well as photos showing my hiearchy and inspector panel. I have gone through several tutorials (Here's a link to the most recent that caused this same error - https://www.youtube.com/watch?v=jbuNElqqO2M), debugged, and scoured the internet for a solution, but continue to get this error (Lines 22 and 34 located in Code 1 - SceneFader.instance.FadeIn("Title Menu"); / SceneFader.instance.FadeIn ("Gameplay");):
NullReferenceException: Object reference not set to an instance of an object ChangeSceneManager.MainMenu () (at Assets/Scripts/Mangers/ChangeSceneManager.cs:25) UnityEngine.Events.InvokableCall.Invoke (System.Object[] args) UnityEngine.Events.InvokableCallList.Invoke (System.Object[] parameters) UnityEngine.Events.UnityEventBase.Invoke (System.Object[] parameters) UnityEngine.Events.UnityEvent.Invoke () UnityEngine.UI.Button.Press () (at /Users/builduser/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/UI/Core/Button.cs:35) UnityEngine.UI.Button.OnPointerClick (UnityEngine.EventSystems.PointerEventData eventData) (at /Users/builduser/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/UI/Core/Button.cs:44) UnityEngine.EventSystems.ExecuteEvents.Execute (IPointerClickHandler handler, UnityEngine.EventSystems.BaseEventData eventData) (at /Users/builduser/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/EventSystem/ExecuteEvents.cs:52) UnityEngine.EventSystems.ExecuteEvents.Execute[IPointerClickHandler] (UnityEngine.GameObject target, UnityEngine.EventSystems.BaseEventData eventData, UnityEngine.EventSystems.EventFunction`1 functor) (at /Users/builduser/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/EventSystem/ExecuteEvents.cs:269) UnityEngine.EventSystems.EventSystem:Update()
Here's my code:
using UnityEngine;
using System.Collections;
using UnityEngine .SceneManagement ;
using UnityEngine .UI;
public class ChangeSceneManager : MonoBehaviour {
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
public void MainMenu(){
//SceneManager.LoadScene ("Title Menu");
//SceneFader.instance.LoadLevel("Title Menu");
SceneFader.instance.FadeIn("Title Menu");
}
public void HowToPlay(){
SceneManager.LoadScene ("How To Play Scene");
//SceneFader.instance.LoadLevel("How To Play Scene");
}
public void Play(){
SceneFader.instance.FadeIn ("Gameplay");
//SceneManager.LoadScene ("Gameplay");
}
}
Code 2:
using UnityEngine;
using System.Collections;
using UnityEngine.SceneManagement;
public class SceneFader : MonoBehaviour {
public static SceneFader instance;
[SerializeField]
private GameObject fadeCanvas;
[SerializeField]
private Animator fadeAnim;
void Awake(){
MakeASingleInstance ();
}
void MakeASingleInstance(){
if (instance != null) {
Destroy (gameObject);
} else {
instance = this;
DontDestroyOnLoad (gameObject);
}
}
IEnumerator FadeInAnimate (string levelName){
fadeCanvas.SetActive (true);
fadeAnim.Play ("FadeIn");
yield return new WaitForSeconds (1f);
SceneManager.LoadScene (levelName);
FadeOut ();
}
IEnumerator FadeOutAnimate(){
fadeAnim.Play ("FadeOut");
yield return new WaitForSeconds (0.7f);
fadeCanvas.SetActive (false);
}
public void FadeIn(string levelName){
StartCoroutine (FadeInAnimate (levelName));
}
public void FadeOut(){
StartCoroutine (FadeOutAnimate ());
}
}
I have been working on this scenefader for days now and continue to run into this issue. I am so close to finishing my game and want to add some polish to it. I would really appreciate some help.
Here are some screen shots of the gameobject as well:
line 25 of your ChangeScene$$anonymous$$anager that you posted here, is empty. I suspect you changed the formatting.. could you specify to which line it takes you if you click on the error message?
Lines 22 and 34 located in Code 1 - SceneFader.instance.FadeIn("Title $$anonymous$$enu"); / SceneFader.instance.FadeIn ("Gameplay");
Answer by Glurth · Jul 05, 2016 at 11:49 PM
I would guess the object you have the SceneFader behavior attached to is disabled or corrupted somehow: preventing it's awake function from being called. This would leave the "instance" member variable uninitialized, and therefore equal to NULL.
The specific error you are getting means that you are trying to access a member of the "instance" variable, but since it's null (not instantiated), it can't store members yet.
P.S. I'm not sure why SceneFader is a monbehavior, rather than just a static class. Seems like not having to deal with the game object would make things simpler: MakeASingleInstance() for example could be a static function, called by any object that need it, in THEIR awake function. Matter of taste I guess.