add properties to enum values

I created a simple Spells script:

public class Spells_scr : MonoBehaviour {	
	public enum Spells {Shock, Ignite, AcidSpray};	
}

I would like to add some properties to the “spells”. In pseudo-code it woul look like this:

Spell.Ignite
{
   _spellPower = 30;
   _spellDuration = 3;
   _otherProperty = otherValue;
   _anotherProperty = anotherValue;
}

In my Player_2controllerScript I have a function which decreases HP of the player, when he’s hit by a spell:

public void LooseHP()
	{
		_health -= Ignite._spellPower;
		
		
		if(_health <= 0)
		{
			_health = 0;
			
			Debug.Log ("Player_2 has died.");
		}
	}

Is it possible somehow to add properties to those enum values and use them like this?

It is not possible for Enum’s to have properties, as @Jamora suggested.
The workaround I would use would be with declaring static instances:

public class MagicEffect 
{
	public string Name {get;set;}
	public int SpellPower {get;set;}
	public float Duration {get;set;}
	// Damagetype ...etc ...

	public MagicEffect(string name, int spellpower, float duration)
	{
		Name = name;
		SpellPower = spellpower;
		Duration = duration;
	}
	
	public static MagicEffect Ignite = new MagicEffect("Ignite", 30, 0.5);
	public static MagicEffect Shock = new MagicEffect("Shock", 40, 1.5);
	public static MagicEffect AcidSpray = new MagicEffect("AcidSpray", 30, 0.5);
}

Then your method (has typo btw Lose instead of Loose) would be :

public void LoseHP()
    {
       _health -= MagicEffect.Ignite.SpellPower;
 
 
       if(_health <= 0)
       {
         _health = 0;
 
         Debug.Log ("Player_2 has died.");
       }
    }

This is not possible in C#, and I assume the same for UnityScript. This is because in C#, enums are just named ints (though they can be any integral type except char).

In other programming languages, Java for example, this would be possible. Enums over there are objects, just like everything else.

Hey there PaxForce, the answer CodinRonin supplied is perfectly adequate, but as I’d already started thinking about it I thought I’d still post what I came up with, while not striclty answering the question of adding properties to enum values it may be a nice solution in your exact scenario of spell properties, and in an inspector friendly fashion too. I like to leverage the power of Unity’s Inspector where ever possible. I may have thought about it too much and come upwith an overly contrived solution but anyway heres is what I would do…

using UnityEngine;
using System.Collections;
using System.Collections.Generic;

//Serializable class to hold spell data.
[System.Serializable]
public class SpellData
{
    public string spellName;
    public int spellPower;
    public int spellDuration;
    //Other fields here...
}

public class Spells : MonoBehaviour
{   
    //Marking list with SerializeField attribute so it saves and shows in Inspector.
    [SerializeField]
    List<SpellData> spells = new List<SpellData>();

    //Dictionary populated in OnEnable to look up spells by name.        
    Dictionary<string, SpellData> spellLookup;

    void OnEnable()
    {  
        //Create and populate the lookup dictionary with the list of spells.
        spellLookup = new Dictionary<string,SpellData>();
        for (int i=0; i < spells.Count; i++)
        {
            SpellData spell = spells*;*

if (!spellLookup.ContainsKey(spell.spellName))
{
spellLookup.Add(spell.spellName, spell);
}
}
}
//Method to get SpellData by name.
//Returns null if there is no spell with matching name.
public SpellData GetSpell(string spellName)
{
SpellData spellData = null;
if (!spellLookup.TryGetValue(spellName, out spellData))
{
Debug.LogError("Spells - No SpellData with name: "+spellName);
}
return spellData;
}
//Indexer, provides a nicer to write way of accessing SpellData
public SpellData this[string spellName]
{
get { return GetSpell(spellName); }
}
}
And then the use of this in a player controller…
//Assign the Spells MonoBehaviour to this in the Inspector.
//Or get a reference in the Start method of the controller.
public Spells spells;

public void LoseHP()
{
_health -= spells[“Ignite”].spellPower;

if (_health <= 0)
{
_health = 0;

Debug.Log(“Player_2 has died”);
}
}
Using a serialized object to hold spell data and having a serialized list of these objects means Unity will show them nicely in the inspector for you to add to or edit. it isn’t static and it does rely on strings to access the data by the use of the GetSpell() method or the indexer but personally I think it is nicer and more flexible than a load of static instances.
[23220-spellscapture.png|23220]*
*