ComputeBuffer.IsValid() is true in editor, but false in play mode

Edit: I feel like a big dummy, it was an “Enter Play Mode (Experimental)” problem, which is new in 2020.1. Enabling “Reload Scene” fixed the issue. If you see people with a similar error in the future, this is a reasonable thing to check. It might be interesting that the error I got from this was about a ComputeBuffer, but that might not be relevant. This is the second post I’ve made in the past few days that I’ve solved after writing it out, is there a way to delete my own posts?

Why would ComputeBuffer.IsValid() return true while in the editor, but false in play mode?

The code in both situations is identical, as far as I can tell:

void UpdateBuffer() {
    if (_data == null) {
        _data = new VolumeData[1]; // An array of structs
    }

    if (_data != null) {
        _data[0] = new VolumeData() {
            isIntersected = 0,
            boundingRadius = boundingRadius,
            stepSizeScaler = StepSizeScaler,
            distToEnter = 0f,
            distToExit = 0f,
            extents = Transform.localScale * 0.5f,
            color = color,
            w2l = W2L
        };
        
        if (_buffer == null) {
            _buffer = new ComputeBuffer(1, _stride);
        }

        Debug.Log(_buffer.IsValid());
        if (_buffer.IsValid()) {
            _buffer.SetData(_data);
        }
    }
}

In the editor Debug.Log(_buffer.IsValid()) (line 22) prints true, and the subsequent if statement is executed. But in play mode it prints false, and the if statement is not executed.

What could I be doing wrong?

Could it be an issue of re-using a ComputeBuffer? But the same code works in older versions of Unity, so I’m struggling to understand why.

If I change the last if check from if (_buffer.IsValid()) (line 23) to if (_buffer != null), then I get an ArgumentNullException, pointing to _buffer.SetData(_data). But that’s inside of null-checks for both _data and _buffer.

And it only happens in play mode, not in the editor. The editor is fine with it either way. This is super weird to me. Does this happen in other cases? That might give hints about what’s going on here.

Help would be hugely appreciated, this is a crucial part of the codebase for my game and I’d like to get it working solid. Ideas for how to debug this further would also be appreciated, it feels like I’ve hit a wall.

Edit: It works fine in builds too, but not play mode.

I don’t think you should feel like that :slight_smile:

There are some cases with the new fast enter playmode settings that require you to write your code differently, but disabling the new option should never be the only solution. There should be a way to write your code to handle our new setting, otherwise it seems like a bug to me, which we’d be grateful for a bug report for.

This page may help you figure out how to re-organise your script code to avoid having to reload the scene:
https://docs.unity3d.com/Manual/ConfigurableEnterPlayMode.html

1 Like