Lighting Bug Discovered


The Bug

For the last week, I've been fighting with a strange bug in the 2D lighting and shadow system from the Universal Render Pipeline in Unity. It took me several days to even figure out what exactly was going on, but in the last couple days I finally cracked it, and came up with a very simple reproduction test project.

In essence the problem is that if you have a 2D sprite with a Shadow Caster 2D component on it which isn't set to self-shadow, when you disable and then re-enable the object, as you would for object pooling, the shadow now covers the object when it shouldn't. See the following screenshot for a good example of the problem. 

The circle on the right is untouched. The circle on the left is exactly the same except that I toggled the active status of the object off and back on again. I've tried everything I can think of to repair this, but it seems like once the object gets into that state, there's no fix for it.

The Bug Report

And so, I took my bug repro project, and submitted a bug report to Unity. No idea how quickly they'll work on it, but I hope it will get addressed fairly quickly, since this seems like a pretty high impact bug, in a feature package that they've been pushing hard for the last year or so.

Honesty, I'm shocked I'm the first one to find it. Which as far as I can tell, I am. I couldn't find any other reports in their system that sounded the same, although it's possible I just wasn't using the right search terms. Anyway, I'll keep you posted on the status of this bug in future devblogs. But now, on to how I worked around it, so that I can continue with my current plans for Crystal Void!

The Work Around

Luckily, for my use case, while I wanted to use object pooling because of the efficiency, it's not really mandatory. The objects that are affected (enemies, pickups, and deadly items) are only spawned (currently) at the start of a round, and there's not a huge number of them. So, my fix for the bug is to simply skip using an object pool for them at all. I reverted my code for those items to directly instantiate the game objects as needed, and destroy them when done.

It's possible this fix may cause some garbage collector hiccups, but I hope not. My highest bulk items, the bullets, can be object pooled, so I'm doing that. The same with with score text that pops up when you kill things or get hurt. No shadowing on either of those, so it was safe to pool.

Hopefully, Unity will fix this issue quickly, and I'll be able to restore the pooling logic for those items. If they don't, and I end up needing to pool stuff, I have some ideas for further things to try, but they're fairly involved, so I don't want to spend the time on implementing them until it's needed.

For now, thank you for your time, and I hope you're enjoying the bit of the game I've got finished so far. Next up, I'm going to be working on the upgrade purchase system, and then probably adding a new enemy type. I should probably also change the enemy spawning system soon, so that more spawn over time as you stay in a given level.

Get Crystal Void

Download NowName your own price

Leave a comment

Log in with itch.io to leave a comment.