Null exception in _unity_self

I’m getting this strange null exception, not really sure if it’s a 2019.3 issue, a Linux issue, or an HDRP issue.

This is caused by the code:

        if (myCamera != null)
        {
            myCamera.RemoveAllCommandBuffers();
        }

Edit:
Seems to have been fixed by also checking if myCamera.commandBufferCount > 0 before removing command buffers

I was wrong, that didn’t fix it. I’m still getting that null exception in _unity_self whenever I remove command buffers

Could you please submit a bug report with a minimal reproduction project?

Looking at the stacktrace, it seems like you actually have a null CommandBuffer attached to the camera - the null isn’t referring to the camera variable.

Sorry the error message isn’t very helpful - a bug report would still be useful to help us improve this. Not sure how/why a null CommandBuffer would be attached to the camera, or even if that’s something Unity should let you do.

It is only happening when a Scriptable Object(SO) is visible in the Inspector. If I do not have a SO visible in Inspector, I do not have that issue.

2 Likes

Yes, I get the same error when a gameobject’s script containing an array of SO’s is displayed in the inspector. I changed it to a List instead of an array and that seems to have solved the problem.

Am on Unity 2022.3.34 (latest) and got this error too. Thought it was random but then realised that it was dependant on what item I had selected before I hit play.
Tracked it down to an exposed class in the component on the selected object

public StateHistory history = new();

The StateHistory class has a [Serializable] at the start so I can see the history in the inspector, but if I remove this Serializable attribute the crash at the start goes away. Seems to be an inspector issue with this kind of class.

[Serializable] // Remove this and the error goes away (and this history is not visible in the inspector
public class StateHistory
{
    public const int historyLength = 10;
    [Serializable]
    public struct HistoryEntry
    {
        public string state;
        public float time;
    }

    public List<HistoryEntry> history = new();

    public void AddToHistory(string state)
    {
        if (history == null) history = new();

        history.Insert(0, new HistoryEntry() { state = state, time = Time.time });
        if (history.Count > historyLength)
        {
            history.RemoveAt(history.Count-1);
        }
    }
}