Search Unity

  1. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice

How can I access an Enum located on a Non Monobehaviour script in another script

Discussion in 'Scripting' started by StarShipMan, Jul 27, 2020.

  1. StarShipMan

    StarShipMan

    Joined:
    Jan 28, 2014
    Posts:
    91
    Hi,

    Been trying to find a solution for this issue for sometime now.

    The below script has an enum Language, which I need to access from the "public class TextLocaliserUI : MonoBehaviour" I would like to basically switch languages.

    Assistance would be appreciated.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class LocalisationSystem
    6. {
    7.     public enum Language
    8.     {
    9.         English,
    10.         French
    11.     }
    12.  
    13.     public static Language language = Language.English;
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.  
    6. [RequireComponent(typeof(Text))]
    7. public class TextLocaliserUI : MonoBehaviour
    8. {
    9.     public Text textField;
    10.     public string key;
    11.  
    12.     public bool english;
    13.     public bool french;
    14.  
    15.     void Start()
    16.     {
    17.         string value = LocalisationSystem.GetLocalisedValue(key);
    18.         textField.text = value;
    19.     }
     
  2. WarmedxMints

    WarmedxMints

    Joined:
    Feb 6, 2017
    Posts:
    1,012
    If you want to use the enum in another script it would just be
    Code (CSharp):
    1. public LocalisationSystem.Language Language;
    If you are wanting to use code based on the setting of the enum declaration in the LocalisationSystem class, it would be
    Code (CSharp):
    1. LocalisationSystem.language
    for example
    Code (CSharp):
    1. switch (LocalisationSystem.language)
    2. {
    3.      case LocalisationSystem.Language.English:
    4.          break;
    5.      case LocalisationSystem.Language.French:
    6.          break;
    7. }
     
    StarShipMan likes this.
  3. StarShipMan

    StarShipMan

    Joined:
    Jan 28, 2014
    Posts:
    91
    Much appreciated, thank you. I was able to get the code working!
     
unityunity