Zuiq Pazu
Zuiq Pazu

Reputation: 295

Setting Properties in Unity Editor

As much as it makes sense I'm trying to use interfaces when working with scripts, something like this:

public interface IConsumable
{
    Sprite Icon { get; set; }
}

However when using this approach, any classes implementing the interface do not show these properties in the inspector and I end up with something like this:

public class TestConsumable : MonoBehaviour, IConsumable
{
    public Sprite Icon { get { return IconSprite; } set { IconSprite = value; } }

    // Hack just to show up in Editor
    public Sprite IconSprite;
}

This doesn't really make sense to me and I was hoping there was a better solution.

Side-note, I'm not using getters / setters exclusively for Interfaces but also for some validation etc.

Thanks!

Upvotes: 0

Views: 4174

Answers (1)

Tyler Day
Tyler Day

Reputation: 1718

By default properties with get/set will not show up in the editor. You need to mark them with the "[SerializeField]" attribute in order to use them in the editor. The reason is that unity will only display types that it can save.

public class TestConsumable : MonoBehaviour, IConsumable
{
[SerializeField]
private Sprite icon;
public Sprite Icon { get { return icon; } set { icon = value; } }    
}

Upvotes: 0

Related Questions