Hello everyone,
My game is working fine in the editor but throwing an error in the very start of the game in the Windows Standalone build. Error:
NullReferenceException: Object reference not set to an instance of an object
at FirstScript+<Fade>d__7.MoveNext () [0x000d2] in <08f0ca6351a0416391806f1389c329f9>:0
at UnityEngine.SetupCoroutine.InvokeMoveNext (System.Collections.IEnumerator enumerator, System.IntPtr returnValueAddress) [0x00026] in <2031488213494187ad8fdf6a91d614ad>:0
This is the script:
using System.Collections;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
public class FirstScript : MonoBehaviour
{
public float waitDuration = 3;
public Image img;
public Object mainMenuScene; // Scene object
private Color fullAlpha;
private Color zeroAlpha;
public bool isFirstScene;
void Start()
{
if(isFirstScene)
{
zeroAlpha = img.color;
fullAlpha = zeroAlpha;
fullAlpha.a = 1;
StartCoroutine(Fade(zeroAlpha, fullAlpha));
}
else
{
fullAlpha = img.color;
zeroAlpha = fullAlpha;
zeroAlpha.a = 0;
StartCoroutine(Fade(fullAlpha, zeroAlpha));
}
}
IEnumerator Fade(Color from, Color to)
{
if(isFirstScene)
{
yield return new WaitForSeconds(waitDuration);
}
float t = 0f;
float duration = 2f;
while (t < duration)
{
t += Time.deltaTime;
img.color = Color.Lerp(from, to, t / duration);
yield return null;
}
if(isFirstScene)
{
SceneManager.LoadScene(mainMenuScene.name);
}
}
}