- Home /
Brain freeze: declaring a prefab inside the same prefab
This might seem like a pretty noobish and common question. And perhaps it is, but I haven't found an approach to the same subject so I hope some of you already faced this dilemma.
I have a simple prefab which, because of a multiplayer necessity, needs be instantiated dynamically.
The point is, this object is not just dynamic in the sense that it can be randomly spawned. The very reference for it in the script does change. So as an example, I have:
...
class explosion
Transform explosiontype;
...
Now my prefabs are in the sort of
nuclear
daisycutter
napalm
but of course since I need to instantiate that prefab at some point (they are assigned into other gameobjects arrays)
I have something like
nuclear prefab => explosiontype => nuclear prefab <= dragged into the public slot in the inspector
daisycutter prefab => explosiontype => daisycutter prefab
napalmprefab => explosiontype => napalm prefab
Well, basically this works. But it feels dirty. I am wondering if any of you faced this dilemma already, and if this is the preferred way to solve this necessity. Again, the 'problem' is the fact that in a multiplayer setting, Network.Instantiate needs a PREFAB reference, whilst the normal instantiate can do with an object.
thanks
As a matter of fact, I now realize it doesn't work:
http://forum.unity3d.com/threads/57312-Reference-to-prefab-changing-to-clone-self-reference
this is so fucking horrible a problem that I don't even know what to look at for a solution, since it's so ridiculously counterintuitive and buggy as a behavior that there was no possible good planning ahead that could have prevented it. I basically would need to change two dozen classes to create a master prefab reference... and even so I would be halfway through a solution.
Hope that some of you unity gurus out there faced this problem and found a viable solution (i.e. breaking prefab connection, perhaps?)
It's not clear to me why the explosion needs a reference to itself. Surely whatever Instantiated the original explosion would already have that, and should be also responsible for creating the network instances too?
@Warwick Allison sorry if it wasn't clear, it's because of a stupid idea behind the classing, where an explosion spawns sub explosions, for which I need the base prefab to be referenced. Sadly, I two other main classes upon this logic, and rewiring them to use a global prefab reference array or list would be plain crazy.
Well if you admit that the idea is stupid and you're too lazy to fix it, it's a little difficult to help, haha. It's UnityAnswers, not UnityHacks.
@Derek Traver lol it is not a stupid idea per se. It's stupid because it stuck me in a problem that should not have existed to start with. And lol @ being lazy. We're talking about thousands of lines of raw code which again would never have needed an unnecessary fix for a buglike behavior. I'm sure that sits well with you too!
Answer by Waz · Aug 25, 2011 at 02:36 PM
At the time the original object is created:
var e = Instantiate(explosion,p,r);
e.explosionType = explosion;
I would even unset them in the inspector, to ensure you get a crash if this is missed anywhere.
I was going to say something like this.. then have e load the right prefab. Although I don't see why he can't just have references to all the explosions prefabs, do a switch on explosionType and instantiate the right prefab right at the source.
Also, he mentioned he's using Network.Instantiate, so in this case he'd have to hit the newly Network.Instantiated "e" object with an RPC to set explosionType.
On second though... operating under the assumption that these explosions being instantiated by a previously Network.Instantiated object (like a missile or whatever), and assu$$anonymous$$g that missile or whatever's rigidbody or transform is being sync'd over the network, there's no reason the explosion needs to be Net Instantiated, each client could just do that locally.
Sounds to me like you are way too shy of refactoring. Refactoring should be done as soon as proven necessary, yet it seems you're letting this cause more knock-on problems than the effort of simply fixing it. With languages like C# and UnityScript with #pragma strict
, refactoring is mostly a matter of making the core change then doing the monkey-work of getting everything to compile again.
It actually works. I had to redesign just half of my classes but this can save you some trouble. Thanks @Warwick Allison