After several game jam coming across gameObject reference error, it was time to do something about it

With a few developers and integrators working on the same project, every game jam we encounter a few recurring issues:
- Someone adds a new feature with new prefab(s) in the scene. Other scenes lack the prefab, and game is broken
- A gameObject that references another gameObject from the scene loses the reference: the script breaks
One root cause links them all: a script (MonoBehaviour) references an external game object from the scene.
Maybe this is a SerializeField that we drag and dropped. Or a FindGameObjectsWithTag or Find to fetch an object. But at some point, the reference is used, and there is nothing behind.
Those habit we had ultimately fails to achieve one key concept: making a modular software, where systems do not directly depend on each other.
Before the jam, I was looking for some pattern we could use when creating our Unity game.
Thankfully I discovered that talk from Ryan Hipple and his article, which made me realize it was possible to build your game around Scriptable Objects.
If you run across the same issues as we did on Unity, I highly suggest you give a look at the article (and the video if you have the time).
What we ended up with is a game where every interaction between our prefabs was made using Scriptable Objects (SO). Suddenly all our prefabs could live independently. When we need to interact between different gameObjects, we know we have to rely on SO.
- Variables that are used by multiple prefabs are now stored in a SO that lives outside the scene and that will never raise a Null Reference Exception.
- Function calls between multiple prefabs rely on SO as well, using some generic event SO that can be emitted or listened to (using an observer pattern)
Here is the kind of design (or, should I say, absence of design) we used before:
And now, making sure prefabs interact only through SO, it is closer to this:
We tried to apply that architecture this game jam for the first time, and we are quite satisfied with it. We still have improvements to make on the generic classes we use as a basis for our variables and events, but it definitely helped us achieve a stabler game this time.
I hope this can help some other developers!
Lastly, we would be grateful if you could check out our game Sol Invictus. See you next jam!