2
\$\begingroup\$

I have a list of enums. I want to add a new item to it without scripting to make them more easily modifiable for game designers. I made an inventory system and I want to define the type of an item. For an example apple is a type of food and stone is a type of object.

Is there a way to group these items in the inspector?

using UnityEngine;
using System.Collections;

public class NewBehaviourScript : MonoBehaviour {
    public const string[] typesStrings = {"Player", "Prop", "Enemy"};
    [System.Serializable]
    public class TypeList
    {
        public enum types
        {
            PLAYER = 0,
            PROP = 1,
            ENEMY = 2
        }

    };

    // MonoBehaviour class
    public TypeList.types selectedType;

    public void Start()
    {
        string selected = TypeList.typesStrings[(int)selectedType];
    }
}

This, however, gives me the following error:

Assets/grouping.cs(5,31): error CS0134: A constant grouping.typesStrings' of reference typestring[]' can only be initialized with null

\$\endgroup\$
3
  • 1
    \$\begingroup\$ Please only ask one question at a time. I removed the second question from your post. But feel free to post it again as a new question. You can retrieve the text and image from the edit history. \$\endgroup\$
    – Philipp
    Jul 13, 2016 at 15:22
  • \$\begingroup\$ To work around that particular error, I usually define my constant arrays as static readonly which is not quite constant (the array contents are still mutable) but close enough for my purposes. \$\endgroup\$
    – DMGregory
    May 8, 2020 at 2:29
  • \$\begingroup\$ @DMGregory enums have many problems so I use ScriptableObjects instead. \$\endgroup\$ May 8, 2020 at 6:14

1 Answer 1

6
\$\begingroup\$

you can use System.Serializable

using UnityEngine;
using System.Collections;
public class grouping: MonoBehaviour {
    [System.Serializable]
    public class class1{
    public enum myEnum {one,two,three};
    public myEnum[] row;
    public string name;
    public int price;
    }
    public class1[] array;
}

useful link 1 for more information:

make your lists functional with ReorderableList

enter image description here

useful link 2 Custom Editors in Unity3D – Part 8: CustomEditor

enter image description here

Unity Editor: Enum Flags as Toggle Buttons useful link 3 enter image description here

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .