Home > Enterprise >  Get enum's value as font style
Get enum's value as font style

Time:10-20

I have created an enum of 3 fonts as shown in script below. I am trying to assign the respective selected font from the inspector window. Unfortunately I do not get the value selected, instead I get an error. What am I doing wrong?

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

public class TextScript : MonoBehaviour {

 public enum fontStyleEnum // your custom enumeration
    {
        Bold, 
        Underline, 
        Italic
    };

    public TMP_Text textMesh; 

    public fontStyleEnum fontStyle;
    
     public void Start(){
        textMesh.fontStyle = FontStyles.fontStyle.value; //I get the error here
    }
}

CodePudding user response:

Variable you are assigning is using enum TMPro.FontStyles and you are trying to assign your fontStyleEnum type.

Assing it any of TMPro.FontStyles enum and it will work.

CodePudding user response:

FontStyles does not have a field called fontStyle.

You have to take your enum and cast it to FontStyles.

See:

public void Start()
{
    textMesh.fontStyle = (FontStyles)fontStyle;
}
  • Related