Abstract method

 if (SpawnedObject.GetComponent<DefaultItem>())
                    {
                        DefaultItem test = SpawnedObject.GetComponent<DefaultItem>();
                      
                    }

DefaultItem is a abstract class. there is a abstract method that changes depending on the class (weapon, healing, etc).

How would I call the new override method without having to list every single item?

Have you tried this?

test.YourAbstractMethodImplementation()

Depending on the concrete implementation of the component the given override should be called.

Also … simplify this to not repeat the same GetComponent call needlessly and to make the code more readable:

var item = SpawnedObject.GetComponent<DefaultItem>();
if (item)
{
    item.YourAbstractMethodImplementation();
}

Oh wait … did you mean to say that your DefaultItem class has methods like Sword(), Axe(), Halberd() and so on depending on the concrete implementation?
If so, that seems odd because you simply need one method Weapon() which, since it is overriden in a concrete implementation, does whatever you need to do for sword, axe, halberd, etc.

There is a method called SpawnItemStats() that gets overridden.

public abstract class DefaultItem : MonoBehaviour
{

    protected ITEMS WorldItem;

    protected abstract void SpawnItemStats();

}
public class MarksmanRifle : DefaultItem
{
 
    protected override void SpawnItemStats()
    {    
        WorldItem = new ITEMS(ITEMS.ItemName.DefaultRifle);
    }
  
}
public class Sidearm : DefaultItem
{
    protected override void SpawnItemStats()
    {
        WorldItem = new ITEMS(ITEMS.ItemName.LightSidearm);
    }
}

What I’m trying to do is call the overridden method without having to specify the item.

  var test = SpawnedObject.GetComponent<DefaultItem>();
                    if (test)
                    {
                      //inaccessible due to its protection level. probably trying to call the abstract method.
                        test.SpawnItemStats();


                    }

It’s inaccessible because the method is protected, so only the abstract class and classes inheriting from that abstract class can call it, but not for example an ItemManager.

Try making it public or internal.


That solved it, thanks.

Most tutorials and examples I’ve seen always keep abstract and override as protected. I didn’t realise making them both public allows them to be accessed without compiler errors.

Probably worth touching up on what the different access modifiers do: Access Modifiers - C# | Microsoft Learn

Following tutorials is good and all, but it’ll benefit you more to understand how things work at a more fundamental level.

Yes, it’s common to keep the innerworkings protected and just add another public method that calls the “internal” protected method. A little bit like “events” in C# which can only be invoked from inside the class itself. Though of course you can make it public and call it directly as well. This is more a philosophical thing. Like Java people who create an interface for literally everything and all references between classes are interfaces ^^.