- Home /
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
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).
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
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!