- Home /
Prefab of a prefab?
Let's say I make a projectile prefab. The prefab gets instantiated in code. It has all kinds of components attached to it with all kinds of tweaked values. Now I want to have another prefab with all the same things, but I just need to change one measly value, such as speed. Is there any way to do this without just creating a duplicate of the original prefab? (which would suck because my new prefab would not get changes made to the original).
Example: Create a prefab called Bullet A, with a script with properties Speed and Damage. Now I want an additional type of bullet (prefab Bullet B) that I can fire, to be just like Bullet A, but with just Damage changed. AND I want it so if I change Speed in Bullet A, that change will also appear in Bullet B because Bullet B does not explicitly overwrite Speed.
Answer by andrew · Aug 16, 2011 at 08:43 AM
when you instantiate the prefab assign it to a variable and then you can easily access values in its components.
for example in c#:
GameObject theInstatiatedObject = (GameObject)Instantiate(thePrefab);
NameOfScriptAttachedToPrefab nameOfScriptAttachedToPrefab = theInstatiatedObject.GetComponent<NameOfScriptAttachedToPrefab>();
nameOfScriptAttachedToPrefab.ValueYouWantToChange = 1.0f;
Sorry but this is hardly a solution. I gave a simple example but apparently that was a bad idea. What if I also wanted my projectile to inflict a different amount of damage, have a different model, a different explosion effect, sounds, etc. If I have to set all these things in code at runtime, then what the heck do I need prefabs for? What is the game designer (non-programmer) supposed to do when they want to change the speed of rockets? Now they have to be a programmer too?
How is this hardly a solution? If you want small changes you have to set them at run-time, if you want large changes you create a new prefab. Now it would be possible to write an editor script that actually creates linked prefabs in the way you describe, but that would take some effort, and would hardly ever be useful.
Answer by roamcel · Aug 24, 2011 at 03:36 PM
There is a very simple and accessible manner to do this which is based in object oriented programming.
Basically you create a base class 'projectile' and then you create all your subclassess which derive from projectile, for example 'bullet', 'rocket', 'cannonball', etc. Each of those will expose their specific variables and functions, or will override the 'virtual' or 'protected' methods declared in the base class, with particular attention to PUBLIC VALUES for example, you can assign BULLET to two different prefabs, and change the public variable CALIBER and DAMAGERANGE to create a 5.56 or a 7.62.
Again, these attributes need not set at runtime, but you can very intelligently make them exposed with public variables in your code. You set them in a prefab and voila. Thanks to inheritance you can even make a 5.56 gun fire a 7.62 thanks to inheritance.
Since prefabs are basically classes (and code) attached to an 'empty gameobject'. They're de facto the same in unity.
I possibly wasn't very exhaustive in my reply, but you can create a very complex nested prefab of prefabs:
1- create prefab named 'gun': it's an empty gameobject with class 'weapon' attached, class which exposes a public variable 'weapon name'
2- create another prefab and call it 'ammocontainer': it's of class 'ammunitioncontainer' and exposes a public value 'ammo' which is an enum type variable with the values "drum", "magazine" or "clip"
3a- create another prefab called drum_prefab and attach the bullets class to it. the bullet class has two public values, magazine size and caliber which you set at 45 and 9.99
3b- create another prefab called magazine_prefab_556 and attach the bullets class to it setting magazine size to 35 and caliber to 5.62
3c- create another prefab called clip_prefab and attach the bullets class to it. set magazine size to 5 and caliber to 7.62
4- create an empty gameobject. attach the three prefabs to it, one nested under the other. attaching different 'magazine' prefabs to a gun will give you a different weapon effectively based from prefabs which you can easily tweak in edit mode. Even if admittedly this prefab and class hierarchy is barely though out (and a bit inefficient and redundant possibly), from code, you just instantiate the 'gun' prefab you made from the steps above, and obviously you just create new prefabs changing the children to achieve different types of weapons.
Has this changed at all since 2011, or is it still the same issue?
Answer by TheEmeralDreamer · Aug 25, 2011 at 05:06 AM
I'm confused what your asking. You seem to contradict yourself. You want a prefab of a prefab.. So why not create a projectile prefab, then create an instance , and place that instance into another prefab.. If that's what your asking yes it can be done.
for instance:
Arrow- Damage 25, Speed 60-
Bullet- Damage 50 Speed- 100-
The script you apply to the prefab will be added at it's default values, for each projectile you will have to tweak it manually but once you save the "tweaked" instance in prefab form, it has inherited everything the first instance had, and saves your changes along with it.
Interesting idea, but once I place the instance of Prefab A into new Prefab B, the connection to Prefab A is lost. Any changes to Prefab A are not reflected in Prefab B.
Answer by mahalo1984 · Jan 26, 2014 at 04:39 AM
This may not be exactly what you had in mind, but it achieves exactly the result you desire: