How do you create a relationship between two gameobjects without inheritance?

Disclaimer: So the title may not be overly accurate but its the closest I could get.

So I am making a strategy game in Unity and the idea is that there will be several types of ‘unit’ and they can choose out of a pool of weapons. These different ‘units’ all in inherit from their unit type then unit type inherits from a basic ‘unit’ abstract class and on the other side it is a similar story, weapon inherits from weapon type which then inherits from a basic ‘weapon’ abstract class. This isn’t the problem, the problem is making the relationship between the specific unit and it’s assigned weapon, in order for the unit class to see what weapon it has and to base its attack on the weapon assigned, accessing the varibles contained within, such as; range, damage, etc.

Because I work best visually I have drawn out a diagram showing what I mean to help those who are still confused as to my question.

So unit at the top is the unit abstract class i have previously mentioned, then human is the unit type then soldier is the specific unit that will be instanciated later on, this (again as previously mentioned) is mirrored for weapon.

It’s a little bit vague because you don’t specify a problem only how to connect the 2. Well if you just want to connect them you need a reference in both instances. Your Unit (i assume all units are going to have weapons) should have a reference to it’s current weapon. So create it something like Weapon currentWeapon. Since i believe any Weapon will have an owner you should create a Unit owner. This may or may not be necessary depending on what you want to do. A weapon may not need to know anything about its wielder (more info would help) or they may not need them every time, so when you call a method in the weapon you could just pass the Unit instance as a parameter, or create a weapon initializer method which takes the wielder’s variables and sets itself up.

Really everything depends on what you want to do.
If you need more info please specify

  • what exactly is the problem

  • which classes inherit from monobehaviour