- Home /
(Similar Prefabs) vs (Single Prefab + AddComponent)
The thing : I have a "Ninja Enemy" concept. But there are several "kinds" of "Ninja Enemy". So right now my Unity Project has as many Prefabs as "kinds" of ninjas. The thing is, the prefabs have quite a lot similarities, but they differ in one component that is the "special ability" The problem is, when I want to tweak somthing that affects all Ninjas it's a drag.
MY solution : is going to be, create a single prefab to instantiate and AddComponent according to the kind of ninja it is.
> ¿What the experts say about this?
> My main concer ¿Any unexpected measurable performance hit about that?
> ¿Is there a waaaaay better method to aproach this?
Bit more Context : I'm making my game, with lots of not excellent decision, some I can update to better, others I rather left them bad. My criteria usually is "If it's taking longer to keep this system than making a new one, change". Because the project is already quite developed.
Excuse my english
I remember reading that Prefabs only save how to change a normal GameObject. If you drag a Prefab in Scene, Unity basicly creates a new GameObject and apply the changes stored in the Prefab. There should be no Performence differences at all.
Answer by neonblitzer · Jun 03, 2015 at 07:30 PM
No need to worry about performance, unless you're spawning loads of them during one frame, in which case you should create them at Start and just enable them at runtime to avoid hiccups.
The design sounds good. If the special abilities get complex, you should separate them into their own GameObjects and make them into prefabs too, and then instantiate and parent them to the ninjas. Easier to manage and modify than hard-coding AddComponent calls :)
Answer by PhoenixInvertigo · Jun 04, 2015 at 07:25 AM
Not an expert, but you should be able to make the Ninja class, then make several classes that inherit from it, and putting the special ability code in the children classes. Then when you need to tweak the entire Ninja class, you're able to do so without having to jump through a bunch of hoops.
This is nice especially if you're used to inheritance-based specialization, as in many "conventional" software projects. On the other hand, Unity as an environment facilitates and encourages composition-based approach, i.e. having features as separate components and dynamically linking them together. It's usually also easier for novice programmers and game designers to learn and get started (GetComponent
versus polymorphism).
Your answer
Follow this Question
Related Questions
Associate objects to a prefab 1 Answer
Problem with AddComponet(SCRIPT) on a Instantiated PREFAB 0 Answers
prefab always instantiates at 0.0.0 1 Answer
Setting the parent of a transform which resides in a prefab error 1 Answer
prefab instantiated by script containing ShadowCaster2D bugs out 0 Answers