Looking for feedback: Accessing class outside of the class and linking elements.

Hello Unity Forums,

I’ve made a dialogue script, but now I want to add a enum type to every sentence (to indicate who is talking through a dropdown menu for easy setup) and a texture2D and access these outside the class. I’m stuck however.

To make it easy to use, I’ve added the below class so I can change the lines every ‘chat type’(character) says, the texture and the name that it displays. See this image.
7127390--851459--upload_2021-5-10_20-43-51.png

Now I’m looking for a way to access whatever is filled in here above and sure the script reads whatever is filled in through this method and display the correct image/name/text.
I’m quite new to classes and have tried it in a struct as well (but that doesn’t seem possible).
I’m looking for a way to add whatever is filled in above into the below script. I’m however stuck trying to access the values in the class outside the class itself. I’m also wondering if i’m on the right track or completely abusing the class system. Any tips are welcome.

ChatType is an enum (terribly named I know)

public enum ChatType
{
    None,
    Sophia,
    Bossy,
    Gerda,
    Robin,
    Lucas
}
    public TextMeshProUGUI textDisplay;
    public TextMeshProUGUI nameTMP;
    public string [] sentenceOld; //added this to make sure the dialogue and typewriter works, but would like to add it to the class (as done below with string [] sentences
    private int index; //added this to make sure the dialogue and typewriter works.
    float typingSpeed = 0.02f;
    bool stopTypewriterBool = false;

    public string [] sentenceOld;

[System.Serializable]
    public class ChatClass
    {
        [SerializeField] public ChatType chatType;
        [SerializeField] public string[] sentences;
        [SerializeField] public Texture2D textures;
    }
    [SerializeField] ChatClass[] chatClass;

    void Start()
    {
        textDisplay.text = "";
        StartCoroutine(Type());
    }

    void Update()
    {
        if (Input.GetKeyDown(KeyCode.Mouse0))
        {
            if (textDisplay.text != sentenceOld[index])
            {
                stopTypewriterBool = true;
                textDisplay.text = sentenceOld[index];
                return; // prevent spam clicking and bugging text out.
            }
            else if (textDisplay.text == sentenceOld[index])
            {
                stopTypewriterBool = false;
                NextSentence();

            }
        }

        if (index == sentenceOld.Length - 1)
        {
            if (textDisplay.text == sentenceOld[index] && Input.GetKeyDown(KeyCode.Mouse0))
            {
                CloseDialogueBox();
            }
        }
    }

     IEnumerator Type()
    {
        foreach (char letter in sentenceOld[index].ToCharArray())
        {
            if (stopTypewriterBool == true) yield break; //quits the foreach loop if bool is true.
            textDisplay.text += letter;
            yield return new WaitForSeconds(typingSpeed);
        }
    }

    void NextSentence()
    {
        //nameTMP.text = ChatType.Sophia.ToString(); //Works!

        if (index < sentenceOld.Length - 1)
        {
            index++;
            textDisplay.text = "";
            StartCoroutine(Type());  
        }
    }

You’re welcome to use classes however you like. If you like you can even put everything in one big giant static class and pretend like you’re an old skool coder. The distinctions of how to break stuff up are entirely artificial and to help you organize it yourself.

NOTE: enums are serialized in Unity as plain old integers. This means once you create assets with Sophia, it does NOT store “Sophia” but rather the digit 1. Therefore it is EXTREMELY fragile and should you ever decide to change the ordering of your speakers in the enum, 100% of all your assets will be silently corrupted.

I would NEVER recommend using enums for anything you intend to serialize to disk. Never. It’s just too easy to make a mistake and not realize it for a long time before you have just made a complete disaster of all of your assets.

I recommend instead looking into using ScriptableObjects. You would have a ScriptableObject for each speaker, and you could even call the Sophia one “Sophia,” and all things that reference her will remain… best part is that if you rename Sophia to Blahblah, it will not break, thanks to how Unity serializes stuff via GUID.

Thank you Kurt-Dekkers,

I’ll try to convert the enum to a scriptable object (not really, but you get what I mean) and try to work from there.

I know what you mean! :slight_smile:

Also, it can even be SUPER simple. Like pretend this is your “speaker” ScriptableObject:

using UnityEngine;

[CreateAssetMenu]
public class ChatSpeaker : ScriptableObject
{
    // you don't even need anything here, just right-click-create
    // instances of this object, and name them.

    // then once you have named them, make this in your script:
    //    public ChatSpeaker Speaker;
    // and you can drag these instances in.

    // THIS IS OPTIONAL:
    // if you like, you can make an image that is their face:
    public Texture2D FaceImage;
    // or use a sprite
    public Sprite FaceSprite;
}

You would right-click-create make these things, one each for Sophia, Bossy, Gerda, Robin, Lucas

You would name the actual asset as their name.