Object Inherit from object?

Hey guys,

I am a total newby in Unity, so pls excuse my maybe “stupid” question, but I tried for hours and couldn’t find a solution.

I am working on a 2d Airhockey game with an AI enemy.

  1. I created a “Player” script, which has a “LivePointsCounter”-var.
  2. Then i created two other scripts, “HumanPlayer” and “AI”, both inherit from “Player” script. I connected those two scripts to a “HumanPlayer_obj” and a “Enemy_obj”.

Now I don’t know how to let every player, no matter if humanplayer or AI, know he has a livePointConter which will be displayed in the HUD and also he has to know his own goal, when the ball enters his goal he should lose livePoints.

I could do that if would code separately for “HumanPlayer_obj” and “Enemy_obj”, but I would like to not repeat my code.

I hope I managed to describe my problem

An example is very welcome.

Which programming language are you using? It will be easier to provide a hands-on example if we know in which domain you’re playing around in :slight_smile:

c# in visual studio :smile:

If you make “LivePointsCounter” in Player.cs as public or protected, then all child classes will also have that variable.

In jeffreyschoch above said manner, I would have a private variable, with a public getter and a protected (or abstract that has to be implemented) setter.
This gives objects outside read-access to the the variable at any time, but only the children can modify its values. And since your decrementing it, maybe even a setter is too much, but then only build a ‘decrementLivePointsCounter’ which only lowers it by one. This one you could put in the super-class.