Skip to main content
This is a Q&A site, so *every* post is a "Question about..." something. You can leave that part out and focus on the "something".
Link
DMGregory
  • 141k
  • 23
  • 258
  • 401

A question about How to use AddComponent and subclassesto add an instance of the derived class, not the base class that defines the method?

Source Link
Sean Carey
  • 694
  • 6
  • 14

A question about AddComponent and subclasses

This is my base class

using UnityEngine;

public class Weapon : MonoBehaviour, IInteractable
{
    public void Interact(Player player)
    {
        player.gameObject.AddComponent<Weapon>();
    }
}

It's extended by different children such as Pistol and M16. How can I make it so that the Interact function of the base class adds the child script instead of the base script?