How to convert interface to a GameObject

I am making a weapon system for my game, and I need to instantiate my weapon for the player to use. The problem is that I have an interface called IPickupable, and then a list of Type IPickupable.

public List<IPickup> allEquipable;

As of now I only have one GameObject in there, which has the interface IEquipable, and IPickup.

The Pistol you see is basically just a Prefab object that has a script called weapon, that inherits multiple interfaces. Mainly IEquipable and IPickupable.

And then for the player, I have another list with every one of his picked up items.

public List<IPickup> inventory = new List<IPickup>();
public IEquipable currentItem;

And as you can see I also have an interface with IEquipable, that’s everything the player can equip and use. The IEquipable go on things like weapons and stuff.

So now I have a system, when the player spawns it automatically gives the player Pistol. Which I find by doing this:

var IitemToEquip = GameController.GC.allEquipable.FirstOrDefault(x => x.PickUpName == "Pistol");

So now I want to convert that interface to the Prefab that inherits that interface. Afterwards I want to check if that interface is an IEquipable, and if so… Instantiate it. But I have no clue how to do that. Thanks for the help.

You should be able to cast to your weapon script, then use the gameObject variable on it.


Or, if that interface is used on multiple types, you can just cast directly to MonoBehavior.

To test for an interface, you can use the C# ‘is’ keyword.

if (IitemToEquip is IEquipable) {