Hi,
Just some musings on prefabs I wanted to capture.
So, I was experimenting with how to best approach prefabs. My example is wanting a missile that also has trail after it. My natural instinct was first to simply create a prefab containing the missile mesh and the trail, and instantiate that. Pretty straightforward!
But doing that means the trail disappears when the missile is destroyed, and that’s not what I wanted. One solution could be hiding the missile mesh and delay its destruction until the trail is done… but that means having the missile object hanging around the scene for a potentially long time. And I feel solving it this way is like I’m compensating for how Unity works, but sacrificing code readability. With the missile taking on this separate concern of the trail it made the code less clear. I mean, at end of the day when the missile explodes I think it makes sense to also destroy it.
So instead I went with not having the trail as part of the missile. I created two different prefabs, and set up a simple Follow script on the trail so it can follow the missile. And then added a **CreateMissile** method that properly instantiates both prefabs and links them up correctly. I think that’s semantically clearer since now the two objects can have their own separate lifecycles without bothering each other.
I initially over-engineered it too far and came across a potential pitfall with this approach, and that is if the linking between prefabs get too obscure. In my first experiments I had a non-monobehavior “manager” class that took both instances and tied them together using events. And the code was really clear! But it was also impossible to see that relationships in the editor, or set it up/tweak it manually during experimentations. So I settled on just having a public **target** property that links to the missile instead.
…I’m not sure any of this is of much interest to anyone, but I’d be curious to hear how you’ve approached things like this and what solutions you’ve come across that work (or don’t work!). I’m new to Unity so it’s hard for me to gauge what are normal solution and what is over-engineered ![]()