Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Inheritance advise => Actor(Combatant) vs Building in RTS game

Discussion in 'Scripting' started by Raistlin2015, Jan 19, 2021.

  1. Raistlin2015

    Raistlin2015

    Joined:
    Apr 4, 2015
    Posts:
    27
    I think I should have two different base classes for inanimate objects like buildings and animate objects like turrets or vehicles. They do share some functionality like health, armor rating and value to the opposing player when it is captured or destroyed. I know this is probably an opinionated "it depends" question, but how did you handle a similar situation?

    Thank You
     
  2. Hosnkobf

    Hosnkobf

    Joined:
    Aug 23, 2016
    Posts:
    1,076
    I think you need one class for the shared stuff and one class for the extended functionality. These classes extend MonoBehaviour and are attached to the corresponding prefabs.

    But this is just one way of doing it. It depends on your general code architecture if that makes sense in your case.
     
    Raistlin2015 likes this.
  3. Raistlin2015

    Raistlin2015

    Joined:
    Apr 4, 2015
    Posts:
    27
    I just smacked my forehead.... Thank You
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,713
    Unity is a component architecture ("has a thingy").

    You are thinking about an inheritance architecture ("is a thingy").

    You may find it helpful to consider your problem without inheritance, as MonoBehaviors are already perfect components that are purpose-built to interoperate with each other while attached to a GameObject.

    You CAN do it with inheritance but I absolutely do not recommend it because of the impedance mismatch with Unity's underlying architectural choices.
     
    Raistlin2015 and PraetorBlue like this.