Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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
0
Question by cijolly · Mar 30, 2018 at 10:19 PM · prefabinheritanceprefab connectionreuse

Patterns for reusing, inheriting from, or extending GameObjects or Prefabs.

Problem

I have a GameObject with a number of scripts attached. I've designed the GameObject to be a 'base' object for moving entities in my game. The scripts handle things that will be common to all physical objects in the game: Being picked up/thrown, teleported, stowed in vehicles, moving in a direction, etc.
Some of the scripts store public GameObjects added via the inspector to instantiate when the object is spawned: The rendered GameObject, the RigidBody GameObject, and the triggers the object uses.

What is a good pattern to extend and reuse this 'BaseMovableObject'? For example, I'd like to extend the base into 'Items' and 'Characters'. Characters would further be extended into the different enemy types.

Proposed Solutions

I have tried/proposed the following ideas:
1. Save the object as a prefab called 'BaseMovablePrefab'. Drag the prefab into the scene, make the changes to give it the scripts a character requires. Save this new GameObject as 'BaseCharacterPrefab'. Repeat for additional child classes/child gameObjects. The problem with this approach is that it doesn't give me a persistent inheritance. Changes I make to 'BaseMovablePrefab' wont be reflected in 'BaseCharacterPrefab', and will have to be manually copied down the inheritance hierachy.
2. Create the base object programmatically. Instead of having a prefab with all the required component scripts attached, have a creation script that will attach all the composing scripts to the GameObject. While this requires a bit more scripting, and changes to this base 'SetupMovableObject' script will happen in any GameObject that has the script as a component.
3. Implement Subclasses as child objects. In this construction, a 'Character' would be a GameObject that would have a 'BaseMovablePrefab' as its parent transform. They would access scripts in the parent object to perform base functionality, and implement their own additional functionality (holding weapons, jumping, etc.) I think this will have the same problems as option 1, in that eventually I'll want to save a character as a prefab, which will create a new prefab version of the 'BaseMovablePrefab' GameObject anyway...
4. Use one prefab, but give it public variable enums that can be set (ie, AI vs Player controlled, Grenader vs Sniper vs Melee enemy type) or 'slots' that can be filled with GameObjects or Components (weapons scripts, movement scripts, etc).
5. Use a separate 'Builder' script that assembles GameObjects with the desired components. Give the builder script lists of the various prefabs (i.e, the triggers and rigid bodies common to all BaseMoveableObjects, and also the models and weapons for specific types of MoveableObject).

Discussion

My inclination is to go with a combination of Option 2 and Option 5. So, I'm thinking I'll do as much as I can programmatically. Also, I'll seperate the scripts responsible for 'Building' and object from those responsible for 'Being' an object. If I can make the script create GameObjects I am using prefabs for at present (GameObjects for use as Triggers and Rigidbodies, primarily) that would be even better.

In past projects, I have gone with option 1, and spent a lot of time manually fiddling with 'Child' prefabs (i.e, prefabs that were created from an existing prefab). If I added a script to the 'base' object, or changed some parameter in it, I'd have to replicate that change manually to all the subtypes further down in the hierarchy.

What are other people's thoughts on creating extensible, modular, reusable GameObjects? Do you use prefabs (maybe with some Unity functionality I am unaware of), or mainly do things via scripting? How do you easily apply changes to a group of similar prefabs?

I like manipulating things via the editor and seeing those changes in a scene, but it doesn't seem to scale well. Are scripts that run in the editor itself (i.e, creating objects as a 'Builder' script is updated) a solution?

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

0 Replies

· Add your reply
  • Sort: 

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

99 People are following this question.

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

Related Questions

Inheritance problem when saving to prefab 1 Answer

Custom Editor not Persisting Changes to Prefab 1 Answer

Unity Programmatically Link Prefab 2 Answers

Serializating a base class array to a prefab 2 Answers

Object reference not set to an instance of an object (Scripts calling objects which only exist in certain scenes) 1 Answer


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