I understand how to use GetComponent in most of the case but I’d like to know how you guys are thinking when you get the following problem (understanding the best practices) :
I have 2 scripts, PlayerScript and MinionScript which have different behaviour. They have a common attribute : team (an enum).
When I search for all the colliders in a range I can get both. My problem is when I want to know in which team a collider is, I don’t know if I have to check the MinionScript or the PlayerScript.
Do I have to make PlayerScript and MinionScript inherit from another script which would contain my team attribute or do you know a more common way to do it ?
You could give different tags. Then determine if its a minion or player. Then use a simple if statement to deside which script to check for the attributea
@chrrust : I was thinking of this way to do it but I don’t specially like using if or switch statements and cast my object into the specific script afterwards.
Thanks for your quick help guys, I’m gonna go with inheritance, that seems to be the best way so
Also consider interfaces if you’re using C#. If players and minions really are variations of the same base type, then inheritance might be the better choice. But if they simply need to both provide the same type of data or methods, use an interface.
Hey Izina, it looks like you already got some good responses.
I’ll just add that, in general, its a good sign that you’re using inheritance properly if you don’t have to cast to the more derived type. Sometimes, especially in Unity, its unavoidable but see if you can’t get all the functionality you need by using overriden virtual/abstract methods from the base.