Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

168
Views
Call function based on instance type not working as I expect

Problem


Currently I'm trying to write some inventory system. The inventory contains several items. The functionality of each item will changed based on the dog it's used on. Most of the time this means it will do nothing, but specific dogs can use specific items.

I figured I would write a base Item class that implements a generic use(Dog dog) function. Then if a specific item wants to apply a certain functionality for a dog, it could override the function with a more specific dog type. For example TennisBall : Item has a use(GoldenRetriever dog). However, having written this it appears as if only the parent function is ever called.

In my Inventory class I have a reference to the dog based on the Dog class. When I log what the type of this Dog is, I get GoldenRetriever. Yet still the parent Item.use() function is called instead of the more concrete TennisBall.use() function. The reason for this is probably that Dog is what the instance is cast to, so it will call that (not quite sure why my Debug.Log still shows GoldenRetriever then).

I've tried specifying some interfaces to solve the problem also, but it seems that the problem persists. My feeling is that this rather has to do with the parameters in the methods than with anything else.

Question


How can I define functions for my derived classes so they are called based on the type of the provided instance? Do I *have* to cast my instance to a specific type? I find this unintuitive since C# already seems to know what the type of the instance is.

Code


Inventory:
public class Inventory : MonoBehaviour {
    ...
    
    private List<Item> items = new List<Item>();
    private Dog dog;
    public int activeItem = 0;

    ...

    public void addItem(Item item) {        
        items.Add(item);  // This is where the TennisBall item will be added
    }

    public void open(Dog dog) {
        open();
        this.dog = dog;
    }

    void Update() {
        if (Input.GetKeyDown(KeyCode.E)) {
            Debug.Log(dog);  // Output: Golden Retriever (GoldenRetriever)
            items[activeItem].use(dog);
        }
    }
}

Item:

public class Item : MonoBehaviour {
    public void use(Dog dog) {
        Debug.Log(dog);  // Still Golden Retriever (GoldenRetriever)
        throw new NotImplementedException();
    }
}

TennisBall:

public class TennisBall : Item {
    public void use(GoldenRetriever dog) {
        Debug.Log("Doing something specifically for a Golden Retriever!");  // This never gets called
    }
}
over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

You can start with actually overriding the use function and using some type-testing in the override:

public class Item : MonoBehaviour {
    public vitual void use(Dog dog) {
        Debug.Log(dog);  // Still Golden Retriever (GoldenRetriever)
        throw new NotImplementedException();
    }
}

public class TennisBall : Item {
    public override void use(Dog dog) {
         var goldenRetriever = dog as GoldenRetriever; // check if dog is of type GoldenRetriever
         if(goldenRetriever != null)
         {
              Debug.Log("Doing something specifically for a Golden Retriever!");  // This never gets called
         }
         // your other logic....       
    }
}
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!