Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 Izina · Nov 26, 2013 at 04:34 PM · c#gameobjectinheritancemono-behaviour

Attaching new script vs. MonoBehaviour inheritance

Hello !

I'm new to Unity and I must say, the community seems to be really good and active so I hope my newbie question won't hurt anyone :)

I'm developing a simple game with two teams. I need to easily compare team of each GameObject to know if they are enemies.

I was considering one of the two following methods :

  • Attach a script 'TeamBehaviour' to all my GameObject which are in a team. That way I can use GetComponent on my GameObject and call IsEnemy() in it.

  • Inherit my concrete class from 'TeamBehaviour' and call IsEnemy() in the concrete one.

Scripts are bugging me sometimes because they seems really close to inheritance (but I get the concept when you don't have to add multiple scripts on one GameObject). So I was wondering when do you need to attach a new script to a GameObject and when you need to make the script atteched to your GameObject inherit from another one.

If you know exactly why or if you have another method, I would really appreciate it.

It's fuzzy in my head, I hope it's not in the post :)

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
0
Best Answer

Answer by Roland1234 · Nov 26, 2013 at 07:23 PM

A script component should encapsulate a single concept or responsibility and only inherit from another component when it extends on the base concept and needs to be able to be referenced by other objects as the base concept.

In your example, your way of thinking... is confusing me. Where do you need to compare if teams are enemies? In the TeamBehaviour script? Or do you have another component that needs to know if 2 TeamBehaviour instances are enemies? I don't see why you would consider inheritance at all, you should be able to solve your problem by using a simple enum field to identify your teams. Or tagging the objects (though I would go with the enum field personally).

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 Izina · Nov 26, 2013 at 09:26 PM 0
Share

Thanks for your answer, sorry my post was a bit confusing.

I changed my TeamBehaviour script for an enum, but I have this problem (which is why i thought of that kinda solution).

I can have multitype target (a player and a $$anonymous$$ion for example), I'm doing a range detection with Physics.OverlapSphere() which returns me an array of Colliders and I want to get only enemy colliders (based on current gameObject $$anonymous$$m).

When I had a script attached to my gameObject, I used collider.gameObject.GetComponent().IsEnemy(...) but now, do I have to check the collider script type and compare its '$$anonymous$$m' attribute after ?

I guess I made it more confusing now :D

avatar image Roland1234 · Nov 26, 2013 at 11:38 PM 0
Share

As far as the original question is concerned, you'd create a new script component vs. inheriting from a previous one when the new component addresses a specific concern or adds specific functionality in an encapsulated and reusable way.

And you would inherit from a previous component when the new component extends on the base concept and needs to be able to be referenced as the base component.

I think you need more detailed help regarding your specific issue, but the forums would seem like the most appropriate venue. If you accept my answer and link to a new thread on the forums I'll join you there and give you better advice (make sure and post some of the code that you currently have so I can get a better idea of what you're talking about).

Cheers!

avatar image Izina · Nov 27, 2013 at 08:17 AM 0
Share

It's great, thank you for your help :)

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

17 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

Related Questions

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

Check if gameobject has script with inheritance 2 Answers

Assigning variables in inspector 1 Answer

How to access gameObject variable script 2 Answers

C# GetComponent Issue 2 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