[C#][Class]List of all instance in class

First, Sorry for my bad english.

Hi ! I created a game where elements are more powerful than others (just like pokemon ) . But I have problem with my classes. I can not choose the type of spell dynamically. I do not want to create script by skill.

using UnityEngine;
using System.Collections;

public class GM : MonoBehaviour
{
    public class Types : Sort { // Class Type
        public int id; // Type id
        public Types(int id) { // Constructor
        }
        static public Types Fire = new Types(0); // First Type etc ...
        static public Types Water = new Types(1);
        static public Types Nature = new Types(2);
    }
}

And the second script ( Sort mean spell )

using UnityEngine;
using System.Collections;

public class Sort : MonoBehaviour {
    public string nom; // Name of the skill
    public int puissance; // Power of the skill
    public GM.Types type; //Type of skill. It is where I need to change the type in inspector for exemple. I need to set the type of each skill.
}

Thank’s in advance.

You’ve done a lot wrong in your setup:

  • Your “Types” class is derived from your “Sort” class which is derived from MonoBehaviour. MonoBehaviours can’t be created with “new”.
  • The Sort class itself contains a “Types” member field which is also a Sort instance (since Types is derived from Sort). This seems to make not much sense.
  • A minor thing is that class names of singular things should have a singular name. Since one instance of your “Types” class represents a single type it should be called “Type” and not “Types”.
  • Your class hierarchy has a circular dependency since “Sort” depends on “Types” and “Types” depends on “Sort”. There are special cases where this might be necessary / wanted, but in most cases it’s an indication for the wrong setup.
  • In Unity it’s usually easier to create assets for certain things. If you need pure data classes, they shouldn’t be derived from MonoBehaviour but from ScriptableObject. You can create 3 seperate assets of the same class which would represent your 3 element types. Those can be dragged onto a field variable of any other asset.
  • At the moment an instance of your “Types” class only has an “id”. The other inherited members aren’t initialized at all (“nom”, “puissance” and “type” ). If you just want to classify a skill into types you might want to use an enum instead.

Using / reusing the same class for different things only makes sense when those things have a lot common properties and represent the same behaviour. If an instance of your “Types” class represents different elements, they all behave in the same way, they just can have different values

So depending on what’s the actual purpose of your “Types” instances it might be simpler to use an enum:

public enum SortType
{
    Fire,
    Water,
    Nature
}

public class Sort : MonoBehaviour
{
    public string nom;
    public int puissance;
    public SortType type;
   // [...]

That way you can choose the type in the inspector. Of course an enum is just a datatype and doesn’t have any behaviour on it’s own. If you want to implement an general “element” behaviour you can create a seperate class derived from ScriptableObject from which you can create assets, one for each element type.

// need to be in the file with name "SortType.cs"
[CreateAssetMenu(menuName = "CreateElement", fileName = "New Element")]
public class SortType : ScriptableObject
{
    public int id;
    public int puissance;
}

Which that class you can use the assets menu in Unity to create new instances of that class as assets. You should rename the asset according to your element (Fire, Water, Nature) and set it’s properties in the inspector (“id” and “puissance”)

public class Sort : MonoBehaviour
{
    public string nom;
    public int puissance;
    public SortType type;
    // [...]
}

Now you can simply drag and drop the right element instance from the assets folder onto the “type” variable of your Sort instances in the inspector.

Firstly, don’t call your class Sort (I know you mean ‘spell’ but it should be named that). Whilst I think it’s allowed, it could get confusing should you ever use the Sort functionality. You shouldn’t really be calling your variable ‘type’ either, as that could also get confused with C# keywords

If I understand you correctly, you want to be able to set Sort.type from within the inspector. If so I would make something like the below (note I just typed it in here. May contain some typos / syntax errors but you should get the idea.

public class GM : MonoNehaviour
{
public enum eSpellTypes
{
WATER,
FIRE,
NATURE,
}

[System.Serializable] //this will let you edit an array of spells from within the editor
public class Spell
{
public string nom;
public int puissance;
public eSpellTypes spellType;
}

public Spell[] spells;

}