- Home /
Multiple Scripts attached to a GameObject vs Inheritance
I've looked through some of the Unity demo projects and I'll see, for example, a player GameObject with a HealthBar script attached to it as well as a PlayerControl script attached. Meanwhile enemy GameObjects may also have a HealthBar script, as well as an EnemyAI script or something like that.
This differs from how I'm used to programming in non-Unity environments where, if I were to do something similar I'd have a single class Entity or something like that which implements common functionality like the HealthBar logic, and then a class Player would inherit from that class and include the PlayerControl functionality, and then another class Enemy would inherit as well and include the EnemyAI functionality. Then the GameObjects would just have the single scripts attached to them.
Is there any benefit to including multiple scripts on a GameObject like I'm seeing in these demo projects, or is it just a matter or preference? I'm sure there are corner cases where one is definitely better than the other and those would be nice to know about, too, but I'm asking mostly for the general case.
Your answer
Follow this Question
Related Questions
connecting my audio volume to a slider 1 Answer
Unity Amiibo Clone 1 Answer
Referencing variables / functions on another script 1 Answer
Can a parent SetActive a child GameObject anymore 2 Answers
GameObject[] Issues 1 Answer