On Object Variable References

Hello, scripters of the Unity forum.

I recently began an admittedly ambitious project: to adapt my favorite TTRPG into a classic, top-down CRPG. So far, it’s been a slow but steady trek forward, but I’ve come across one problem so far that I cannot seem to work around. Specifically, I would like to know how one object variable could then refer to multiple different types of objects instead of only one. Here’s an example…

In the tabletop game, it’s possible for players to wield many basic world objects as improvised weapons (think chairs, crates, barrels, doors, mugs, etc). I have a method that handles equipping weapons to a player’s hands, but it ONLY works with objects designated with the weapon class (called BaseWeapon), not those designated worldobjects (class called BaseObjects). Here’s how the method is laid out:

public static void EquipWeapon (string _player, BaseWeapon _weapon, int _hand)
    {
        bool _canEquip = false;
        string _error = "";
        int _equipType = 0;
......

QUESTION 1: Unless I am mistaken, I HAVE to specify the _weapon variable’s class in the method, right? I would prefer it could somehow be generic when called, but then set to a specific type shortly afterwards. I could then use the code appropriate for wielding a standard weapon in one half of the script, or switch to the other half when working with an improvised weapon instead. I know I can have two methods with the same name with just a change in the object class, but that leads to a lot of redundant space.

QUESTION 2: Similarly, I would like it to be possible for my player’s main weapon slot (invMainHand) to be either a BaseWeapon object or a BaseObject object. As it is now, I have to designate invMainHand as either of these. I get that I may be completely misunderstanding/misusing things here, so I am completely open to someone pointing me to a better approach. If we go back to the primary objective: how do I allow a player to wield both standard weapons and world objects as weapons without having to resort to ugly stuff like switching out UI elements behind the scenes and other jank?

Here’s some code of what the player character variable declarations look like…

public class BasePC : BaseThing
{
        ....
 
      // Inventory
      public BaseWeapon InvMainHand; //want this to be either BaseWeapon or BaseObject
      public BaseWeapon InvOffHand; //same
      public BaseArmor InvArmor; //this is fine
      public List<BaseObject> InvItems; //need this to be ANY type of object OR its children, not only world objects on their own
        ....

Some notes before concluding:
[ ] As it is currently set up, I have a parent class called BaseThing, from which all child classes stem from. Both BasePC and BaseObject refer to BaseThing. From there, BaseWeapon and BaseArmor refer to BaseObject.
[ ] I know I COULD just have BaseObject have every variable, including weapon damage and armor defense bonuses, but that seems extremely bloated and disorganized. I thought the whole point of this system is to compartmentalize every object type into the bare essentials, so I would really like to keep it organized in a manner similar to what I have now.
[ ] I also know I could make every improv weapon come from the BaseWeapon class, but I would prefer to not categorize stuff like doors, crates, and such as weapons. I suppose that’s a strange line to draw, but that’s how it is. If there is another way, I’d prefer that instead.
[ ] In the future, I want to be need to be able to have other objects equipped in the player’s hands that are neither weapons nor world objects, but other items that must be wielded first. So, there’s yet more of a reason to find a way to put many different types of objects in the player’s hands!

Again, I recognize I could be easily be overlooking something here, that I likely am being too stubborn in trying to fit a solution into a very specific box. I’ve tried searching around for how to tackle the problem and can’t figure it out. I know that part of the issue could be that it’s an unorthodox issue, at least as far as my specific needs go. Most of the time, characters in RPGs wield weapons that are simply thought of as weapons. This is my first real foray into c#, so I know that doesn’t help. I want to learn, though.

Sorry for rambling. Feel free to ask for any follow-up information you may need.

Have you heard about C# interfaces? They play very nicely with Unity’s Component architecture. I almost always reach for interfaces versus inheritance.

Using Interfaces in Unity3D:

https://discussions.unity.com/t/797745/2

https://discussions.unity.com/t/807699/2

Check Youtube for other tutorials about interfaces and working in Unity3D. It’s a pretty powerful combination.

Hey. Thanks for responding. While I didn’t ultimately use your suggestion in solving this specific problem, I did nonetheless appreciate your help all the same. Interfaces do seem quite useful, but I admit I have much to learn when it comes to how they work (and what they’re ultimately best at).

If anyone was interested in what I did in regards to the original questions, it didn’t end up too complicated. I just changed improvised weapons to be considered weapons so that they could be sent to the same equipWeapon method (which is then separated into two halves - one for standard weapons and the other for improvs). Doing this solved both problems at once.

Another important discovery I made was about casting. I learned that it’s possible to change how an object is sent to a method by casting is as a parent object instead. Thankfully, while the unique variables tied to the child become briefly inaccessible when sought directly, you can just change it back into the child form if you need to change something again. It’s a somewhat clunky fix, but it works all the same. Hope this could help anyone else having similar issues.