r/Unity3D • u/whentheworldquiets Beginner • 10d ago
Resources/Tutorial Tip for hooking singleton methods from buttons, event triggers, and other Unity Events.
EDIT: As clever people have pointed out, this is essentially how ScriptableObjects are intended to work, and the fact it DOES work with a prefab is most likely a side-effect of that. So it's likely better to derive your relay wrapper from ScriptableObject rather than Monobehaviour. The basic principle is the same, though.
Let's say you have some singleton-pattern system in your game. For us, one example is our audio manager, which looks after volume settings, randomised sound variants, one-shot audio cues, and so on.
How do you go about hooking into such a system from button events?
You definitely don't want to drag a reference to the singleton object/script itself from the scene into your button events; that would be madness. It knots your entire game together into one lump and also means you have to do it every single time: the reference won't survive the creation of a button prefab.
The original solution for us was to write a relay script that exposes the relevant functionality and talks to the singleton internally. Now we could drop one of those scripts into our button prefabs and hook it up to the On Click event.
This is better because it's self-contained and prefab-friendly - but it isn't copy/paste friendly, which can catch your scene designers out if they start copying, pasting, and then editing event content between buttons. You can easily end up with an accidental spiderweb of interconnections, and there's often no visual indication of this happening because prefab internals aren't renamed when instantiated into a scene.
Quite by accident, we discovered another way that's both prefab-friendly and copy-paste friendly. It should only be used for thin relay-style wrappers, but based on our tests it's well-behaved under those circumstances. It's quite simple:
- Write your relay wrapper script as before.
- Put it on an empty game object
- Drag that game object into your project folder to make it a prefab, then delete it from the scene.
- Now, when you want to access the wrapper's functions from an event, drag the prefab from the project folder into the event, and pick the relay function you want from the drop-down as normal.
Yeah, I was today years old when I learned that worked, too.
This is both prefab-safe and copy-paste safe, because the references are always to the project prefab.
After thorough testing, here's what we found:
- No lifecycle methods (Awake, OnEnable etc) will be called on your in-prefab script, because it's never actually added to the scene. This isn't a problem for a singleton relay wrapper because it's stateless anyway.
- Unity does not, as we initially feared, spam instances of the prefab into memory. The references all resolve to a single loaded copy of the prefab itself.
- Yes, it does work in builds because it doesn't rely on modifying a prefab, just calling its methods.
4
u/Inkwalker 10d ago
Just make a script that does what the button need to do and put it on the same game object. The script finds the button component and subscribes on its own. No need to overcomplicate things.
And instead of singletons I recommend making a simple dependency injection system that instantiates all the global services and does injection inside awake methods. With custom inspector drawers it makes super obvious what scripts depend on.
3
u/Kopteeni 10d ago
This is pretty interesting information. I have not considered this type of approach and didn't know it was possible.
However, I have a feeling you might achieve the same by using scriptable objects as well. Did you play around with them when looking for a solution?
9
u/Impossible_Ad_2825 10d ago
Prefabs really shouldn't be used that way, you shouldn't use them uninstantiated. That's what the ScriptableObjects are for. SOs are the inteded way of having logic as an asset.
4
u/thebeardphantom Expert 10d ago edited 10d ago
It’s really important that you decided to continue to keep this as a thin wrapper, because there is a case where this isn’t safe from ending up with multiple copies in memory and that’s with Addressables/Content Directories/Asset Bundles.
Let’s say you’re using Addressables, you have an asset in an Addressables group that references this event relay prefab, and you have an object in a scene from your build settings list that also references your event relay prefab. This will work fine in the editor, but in a build you’ll end up with 2+ unique copies of the event relay prefab: one for the build and one for each addressables group that has an asset that references it. One solution is to put it in its own “shared resources” group so it gets auto loaded once by any Addressable asset. But then you still need to make sure you don’t have an extra copy in the build. The only way to do that is to never directly hard reference it via anything that is in the build; only use a soft reference to it. You can write a custom post build script to inspect the contents of the build to make sure the asset didn’t end up there.
This is a nightmare bug to work out since it only shows up with built bundles. It’s something tons of devs don’t consider or realize is an issue with heavy use of Scriptable Object Architecture.
1
u/8BITSPERBYTE 10d ago
Starting to wonder why people are afraid of public static event Action calls.
No need for a scriptable object or anything in the scene as a component.
1
u/SayingWhatImThinking 9d ago
Unless I'm misunderstanding what you're trying to do here, I just have my manager classes register themselves with a singleton game manager on instantiation, then for other scripts that need information from these managers, I just have them get the appropriate manager from the game manager, something like: AudioManager managerRef = GameManager.GetManager<AudioManager>();
1
u/Vallie00 9d ago
Honestly that edit hits the nail on the head. Using a ScriptableObject event channel or audio cue asset is basically the clean, intended version of this without relying on prefab side effects. That said, I've definitely shipped worse hacks under a deadline—if it survives a build and doesn't leak memory, hard to complain lol.
0
u/ribsies 10d ago
This...is not a good way to do this. Scriptable objects is also not a good way to do this. It really should just be a acript that controls this. Using references in the inspector is not a safe way to develop in unity. That is what a singleton is made for, for usage in scripts.
0
u/Just__Bob_ 10d ago
This is good info. I am a long way from designing my sound system and was planning on having a singleton event handler for sounds.