NullReferenceException after build project for PC, Mac & Linux Standalone

Hi there! I have a problem with the null reference exception. In the engine itself, everything works correctly, nothing crashes, but when I render a development build and run game, in the main menu i get immediately crashes.

UnloadTime: 1.202000 ms
NullReferenceException: Object reference not set to an instance of an object
at SongManager.StartPlaying () [0x00001] in D:\06UnityProj\2021\02feb\space, Rocks, and other stuff\Assets\scripts\SongManager.cs:32
at UploadManager.Start () [0x00011] in D:\06UnityProj\2021\02feb\space, Rocks, and other stuff\Assets\scripts\UploadManager.cs:35

SongManager looks like:

private AudioSource _music;

public void Start()
    {
        _music = GetComponent<AudioSource>();
    }

    public void StartPlaying()
    {
        //Debug.Log("Play: " + _music.clip);
        _music.Play();
    }

And my UploadManager looks this:

[SerializeField] private SongManager songManager;

public void Start()
    {
        songManager = FindObjectOfType<SongManager>().GetComponent<SongManager>();
        songManager.StartPlaying();
        SetRandomColorToScannerLight();
    }

Please, if someone understands this, tell me where I could mess up.

And one more point who knows, too, tell me, I have a player rotation works through input.GetAxis (“horizontal”), in the engine it works correctly, and in builds it turns very slowly. Tell me if you had such a thing and how you repaired it.

Well, here is the deal. You have two scripts running code in Start. SongManager needs to get AudioSource and assign it to _music before UploadManager calls StartPlaying().

In the editor, SongManager runs first. In the build, UploadManager runs first. This is your likely source of the null error.

Possible fixes, you could change the execution order in Unity to make sure that SongManager runs first, however, an easier way is just to make your _music a SerializedField and drag and drop the AudioSource into it instead of getting the component in Start.

[SerializeField] private AudioSource _music;

So change the code for _music, drag and drop the component into the box in the inspector. Then comment out or remove the Start method in SongManager and see if that fixes it.

Now, this is assuming what you showed is the source of the error because your error code appears to point to other line numbers and you didn’t show all your code, so I’m just making an assumption based on experience.

To note if you add Debug.Log calls to both Start methods as the first line, you should see the order they run in.

2 Likes

God, man, thank you, now it’s really works, you’re a wizard!