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
0
Question by PlayCreatively · Dec 18, 2016 at 05:59 AM · componentinheritanceinterfacepolymorphismhealth

How should I give gameobjects health?

I've been watching allot of videos teaching me about inheritance, interfaces, polymorphism etc, and I'm thinking about starting on a new project using what I've learned to make a bigger game than I've ever made before, but I don't know what tool I should use to do what because there are endless ways to do basically anything, and I want to go the most practical route.

I thought about implementing inheritance by creating a humanoid class as the base class, containing all of the basics like pickUpItem() and takeDamage().

However, what if I want more classes, other than just humanoids, to be able to take damage? Should I make one component that deals with health and spread it on everything I want to be able to take damage? or should i make an interface like IDamagable that other classes that I want to be able take damage implement? or should I go a bit extreme and make every single class that should take damage inherit from a class that contains a function that deals damage?

If I decide on using either a component or an interface should I remove the takeDamage() function from the humanoid class and make them use a component or an interface like all the other classes/objects do?

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
0

Answer by Laiken · Dec 18, 2016 at 06:36 AM

I normally decide how I write my code based on this:

1) what will make it harder for bugs to appear

2) what will make the code more easy to understand in case I need to go back to it to change later

3) what I am more confortable with

In your case I would probably make a script that contains health and other data that the objects that have health also have and name it UnitStats or UnitInfo. Just because it matches the 3 conditions better than other alternatives (I look at the script name and I already know exactly what it is about and it will be easy to understand and make changes). But you might be more confortable with another approach

But seriously. I think it's one of those things that you learn better when you do it the wrong way. It makes you give more value and feel better when you know exactly what you are avoiding when you are writing your code the "correct" way

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 PlayCreatively · Dec 18, 2016 at 07:31 AM 0
Share

You're right. It feels allot better when you understand why you're writing the code a certain way. However, I don't feel comfortable going into a project, knowing that I will be wasting allot of time doing it incorrectly and in the end having to redo it all over again, that's why I hesitated to start right away.

One question tho: Isn't it allot more costly having to run a getComponent every time to check if an object has a health component ins$$anonymous$$d of an inheritance or an interface where I $$anonymous$$NOW that it can take damage? Is there perhaps a better way of checking if it can take damage without running getComponent?

avatar image sandpixels PlayCreatively · Dec 18, 2016 at 10:21 AM 1
Share

I'd say you should just run GetComponent everytime until you find another way of shortening the code process. also if you do ever get stuck on a code just send a ask away on the Unity Answers forums, people are bound to help :) (Just make sure to google your situation first otherwise people might tell you it's a duplicate question :p ) Also I highly suggest you add a comment ( "//message" ) under every code you do describing what it does so you can come back later to it and know what it's supposed to do, it also help people trying to fix your code cause it tells them what you're trying to accomplish.

avatar image Laiken PlayCreatively · Dec 18, 2016 at 04:10 PM 1
Share

I know exactly what you mean! I lost a lot of time before because of stuff like that and those 3 guidelines I follow are what I concluded based on my previous mistakes! But I think that, unless you have a lot of experience with program$$anonymous$$g, that kind of time losing will be unavoidable, but you will learn from it.

About the performance thing. You will probably not get performance hits if you are not using lots of GetComponents every frame. I heard some times that you spend more time worrying about optimization before the game is ready than if you fix the parts of your code that are giving performance hits with the profiler after the game is ready.

But if you want to see what is better, you can do this:

 public int loopAmount = 1000;
 
     void Update()
     {
         for (int i = 0; i < loopAmount; i++)
         {
             Health health = GetComponent<Health>();
         }
         Debug.Log(Time.unscaledDeltaTime);
     }


it will use GetComponent 1000 times per frame (or whatever amount of times you decide) and see the time it takes for that process to happen. After that do the same using the other method you want to compare and see if there's noticeable difference. But again, if you are using it only when you need to get the health from someone, it will matter little if you use GetComponent or not.

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

61 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

Related Questions

An OS design issue: File types associated with their appropriate programs 1 Answer

Accessing interface in a derived class through the parent class 0 Answers

How to change variables from another script? 1 Answer

Instantiating an object from its interface 3 Answers

How to set an object reference field in the Inspector 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