• 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 $$anonymous$$ · Jul 29, 2018 at 03:49 AM · typeattribute

How to add "traits" to a to a type?

By traits, I'm talking about chunks of code that would modify a type to give it some uniqueness. So let's say I have a type called Unit, and I make a new Unit called Pyro. The Pyro, unlike a more generic Unit such as Brute, or Swordsman, has the special trait of igniting every enemy it attacks. Since the Pyro is the only Unit that can do this, it wouldn't make any sense to put the code for igniting enemies in the base Unit type class, and I could have potentially hundreds of traits, some which might increase the Unit's defense by five, and some much more elaborate. Each trait would also need to hold info for which other traits it interacts with or conflicts with, so maybe each trait should be a type? I'm simply asking for the best way to execute this so I don't use my own stupid logic to do something dumb. Think of traits in a Rogue-Like game, or unit upgrades in the newer Civilization games. Thanks.

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

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by JVene · Jul 29, 2018 at 05:34 AM

It is good that you defined your meaning for the use of the word traits, because in other languages that has a meaning quite different than your intent, expressing a paradigm not directly supported in C#, and of no importance to your question.


The subject threatens to get too deep for a post. Your asking about object design, and it can become quite a study. In your post you ask if each trait should be a type. By that I take you meaning to be a class. A class has a type, and some types are structs while others are built in.


If Pryo has a unique behavior for attacking, you could implement that from an abstract method named Attack(), but you'd have to know that all attacks would suffice from such a call. If there were magical Units that could cast spells, they may not be characterized so generally as an Attack() function, and non magical Units wouldn't cast spells. I give this example notion to show how this can influence a design. You wouldn't want Unit to have to account for all possible interfaces itself, many which may not be applicable and taken together comprise considerable knowledge of the derived class that Unit should not bother to have in the first place.


This may be why you've asked about making these concepts their own classes (traits should be a type). You've considered it may be smarter to consider the component design pattern as a means of building character behaviors and characteristics. That is likely best.


Consider how Unity uses components to enable the configuration of a GameObject to be a simple sphere or a complex mesh, to have children with related colliders and to attach code. I don't suggest your character behaviors and characteristics derive from MonoBehaviour, you probably don't want the weight or need its services, but you may require Unit to accept components of your design which define the character in modular plugin attachments, as components do, to build up the unique design of a character.

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 $$anonymous$$ · Jul 29, 2018 at 07:55 PM 0
Share

Thanks, very comprehsive.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

87 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

Related Questions

Can I refer to a custom abstract class as a MonoBehaviour property? 1 Answer

"Restart " C# script that has [ExecuteInEditMode] attribute and Start() function. 0 Answers

Finding a method by custom attribute and passing to a UI button 1 Answer

How to add a decorator to a custom editor OnInspectorGUI()? 0 Answers

Typing Game script 3 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