Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
2
Question by 3j- · Aug 16, 2011 at 01:50 AM · prefab

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

4 Replies

· Add your reply
  • Sort: 
avatar image
1

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;



Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image 3j- · Aug 16, 2011 at 04:54 PM 1
Share

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?

avatar image Joshua · Aug 25, 2011 at 08:24 AM 0
Share

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.

avatar image
0

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.

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image 3j- · Aug 24, 2011 at 11:25 PM 1
Share

This is code inheritance, not prefab inheritance.

avatar image roamcel · Aug 25, 2011 at 04:40 AM 0
Share

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.

avatar image gdbjohnson · Jan 21, 2015 at 07:27 AM 0
Share

Has this changed at all since 2011, or is it still the same issue?

avatar image
0

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image 3j- · Aug 25, 2011 at 05:21 AM 1
Share

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.

avatar image
-1

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:

http://cardboardkeep.com/dev-blog-inheritance-in-unity-3d/

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

8 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Any reason why script vanishes from prefab when instantiated? 2 Answers

Changing Animation Via Script on Prefab 0 Answers

How to load enemies according to enemy list? 1 Answer

How to make TCG Deck (Instatiate based on Prefab) 1 Answer

Dragging the player to prefab 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges