Windows Standalone build not working in PC but working in editor

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);
        }
    }
}

It looks like the error is in the coroutine. From looking at it, it’s probably mainMenuScene that is null. Why is mainMenuScene an object instead of just a string with the name as it’s value?

Either way, solving null issues is.

  1. Identify what is null.
  2. Identify why it’s null.
  3. Fix it.

Now, if it works in editor but not in a build, it’s possible an order of operation error. But, it’s hard to say. Only you can solve why something is null with the info provided.

1 Like

Yes, mainMenuScene was null because for some reason scenes are not loading in the build as expected, maybe it is by design. I attached Scene to the mainMenuScene. In case scene name changed I don’t need to rename it in the script. And it’s makes my work easier. But I guess I need to modify my scripts and need to manually insert strings.