Okay guys, first help request and this one is driving me insane! Bear in mind that I'm still very new at this so please use simple vocabulary, and don't be so harsh.
I'm using unity, C# and I'm trying to make the scenes fade in and out programmatically.
I'm pretty sure that what I'm about to say will bring many facepalms but I don't care, I am pretty damn sure that there are easier and more effeciant ways to make fading scenes but what I want to know is
why my approach not working, rather than finding alternatives for the time being.
What I did was create a UI Panel that pans over the entire screen and wrote a script that makes the alpha of the panel change depending on the number of seconds. This works perfectly well. But the problem is that I can't click on any buttons as long as the canvas containing the panel is activated.
I decided to prefab the entire canvas and attach it to my Levelmanager empty gameobject. I wired it up and wrote two co routines for fading in and out, and referenced the fade in coroutine to the start and the fadeout one to my load level method. What I had in mind was deactivating the panel (or specifically, the canvas) as soon as the fadeout finishes and reactivates when the load next level method is called. First part worked amazingly well... Second part... Not so much.
I dug around and quickly found out that once you deactivate an object it's quite a hassle to reactivate it. I didn't care to use another empty gameobject and parent it to the canvas and do a foreach statement. So I thought about instantiating and destroying the canvas whenever I need it. Yet again, the first part worked wonderfully, but instantiating it again was more of a hassle since it was detached from the levelmanager as soon as it was destroyed.
I decided to enable and disable the panel instead but that did not work at all as I got a Null reference error and I still don't understand what I did wrong. I looked around but none of the links helped me so I decided to do my final solution.
I decided to move the two coroutines to the panel script and make the gameobject destroy itself, that way it will continue to stay attached to the level manager. I wrote two methods to call each coroutine and use these call these methods from the level manager script.
and I get this error:
NullReferenceException: Object reference not set to an instance of an object
sLevelManager.Start () (at Assets/Scripts/sLevelManager.cs:20)
here's the script for the panel:
Code:
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class sFader : MonoBehaviour {
public float fadeSpeed;
public Image fadePanel;
private float fadetime;
private float alpha;
private int fadeDir = -1;
private Color color;
void Start () {
fadePanel = GetComponent<Image> ();
}
void Update() {
alpha +=fadeDir * (Time.deltaTime/fadeSpeed);
alpha = Mathf.Clamp01 (alpha);
fadePanel.color = new Color (fadePanel.color.r, fadePanel.color.g, fadePanel.color.b, alpha);
}
public float FadeDirection (int direction){
alpha = -direction;
fadeDir = direction;
return (fadeSpeed);
}
public void fadingin() {
StartCoroutine ("fadein");
}
public void fadingout(){
StartCoroutine("fadeout");
}
IEnumerator fadein () {
fadetime = FadeDirection(-1);
yield return new WaitForSeconds (fadetime);
Destroy (transform.parent.gameObject);
}
IEnumerator fadeout () {
fadetime = FadeDirection (1);
print (Time.deltaTime/fadetime);
print ("line 3");
yield return new WaitForSeconds (fadetime + 0.5f);
print ("line 4");
Application.LoadLevel (Application.loadedLevel + 1);
}
}
and the level manager script:
Code:
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class sLevelManager : MonoBehaviour {
public float loadnextscene;
//fading variables
public GameObject Fading;
private sFader fader;
private float fadetime;
void Awake () {
Fading = Instantiate (Fading, transform.position, Quaternion.identity) as GameObject;
fader = Fading.GetComponent<sFader> ();
}
void Start () {
fader.fadingin ();
if (loadnextscene== 0) {
return;
} else {
Invoke ("LoadNextLevel", loadnextscene);
print ("invoked");
}
}
public void LoadNextLevel () {
Fading = Instantiate (Fading, transform.position, Quaternion.identity) as GameObject;
fader.fadingout ();
Reset ();
}
the level manager script is very messy I know, but it should still work no? what am I doing wrong?