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.
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());
}
}