Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question: How to allow a list to take derived classes

Discussion in 'Scripting' started by Apox2060, Mar 1, 2015.

  1. Apox2060

    Apox2060

    Joined:
    Feb 26, 2013
    Posts:
    8
    I have a class Skill that inherits monobehaviour and a class Attack that inherits Skill.

    I then have a list of Skills but it doesn't let me add Attack to the list.

    Any idea why this isn't working or how to get it to work. Do I have to use an array instead of a list?
     
  2. Apox2060

    Apox2060

    Joined:
    Feb 26, 2013
    Posts:
    8
    I was able to do this by adding the Attack skill to a Game object and saving it as a prefab. I was then able to drag the prefab into the list of skills.
     
  3. bloomingdedalus

    bloomingdedalus

    Joined:
    Aug 13, 2012
    Posts:
    139
    Because "Skill" inherits "monobehavior" - it will always have to be attached to a game object to function. Personally I would try to keep stats and game mechanics classes separate from inheriting from monobehavior and just wrap them in monobehavior classes as needed, but it depends on what you're doing.