Search Unity

Can't add derived classes to a list of base class on prefab inspector

Discussion in 'Prefabs' started by alt9619, Jul 2, 2022.

  1. alt9619

    alt9619

    Joined:
    Jul 2, 2022
    Posts:
    2
    I'm making a battle system involving decks of cards that contain 1 - 4 actions. I'm currently planning to create these cards as prefabs that I can drag actions into to populate. The actions are defined using the following base/derived class scripts:
    (Base Class)
    Code (CSharp):
    1. public abstract class Abillity : ScriptableObject
    2. {
    3.     public string move_name; //name of move
    4.     public string desc; //short description of move
    5.     public int base_power;
    6.  
    7.     public abstract IEnumerator DoAbility();
    8. }
    (Example derived class)
    Code (CSharp):
    1. public class Swipe : Abillity
    2. {
    3.     public void setup()
    4.     {
    5.         move_name = "Swipe";
    6.         desc = "A slash using a light weapon or claws";
    7.         base_power = 45;
    8.     }
    9.  
    10.     public override IEnumerator DoAbility()
    11.     {
    12.        // this will contain specific instructions executed when using this action
    13.     }
    14. }
    Found this code from this thread: https://answers.unity.com/questions/873084/best-way-to-handle-multiple-moves-like-pokemon.html

    Here is the scriptable object I'm using for the card prefab:
    Code (CSharp):
    1. public class Card_Prefab : ScriptableObject
    2. {
    3.     public string card_description;
    4.    
    5.     public List<Abillity> actions = new List<Abillity>();
    6. }
    This is how the hierarchy looks when the prefab is generated:
    upload_2022-7-2_17-54-6.png

    I am unable to drag the script for "swipe" into Element 0. I'm sure it's something I've misunderstood since I'm relatively new to unity, but is this approach possible? If not, is there an alternate approach to this that I can try?

    Thanks
     
  2. alt9619

    alt9619

    Joined:
    Jul 2, 2022
    Posts:
    2
    I ended up creating a dictionary (<string, Ability>) and creating an list of strings to add to my card prefabs. When the card is requested, the correct ability is found base on the strings in the cards list
     
  3. Kwenchinu

    Kwenchinu

    Joined:
    Sep 20, 2023
    Posts:
    1
    Necroing but I'm trying to do exactly that, sad nobody replied, I don't understand what's wrong logic wise, there's no error either or anything.

    Edit: I guess since it's not possible to have a List of multiple types, this being allowed would, in fact, allow just that even if they derive from the same parent.
     
    Last edited: Sep 20, 2023