- Home /
help simplifying my dialouge script
so, i made a diolouge script aand it works but it's a pain to work with so i was wondering if there was some way to simplify it so that way instead of having 4 arrays we can have less using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; using UnityEngine.SceneManagement;
public class DiolougeManager : MonoBehaviour
{
public string[] diolouge;
public string[] name;
public Sprite[] icon;
public bool[] isPlayer;
public Text talkArea;
public Text nameArea;
public Image characterOne;
public GameObject characterOneRoot;
public Image characterTwo;
public GameObject characterTwoRoot;
public int diolougeCurrentlyOn;
public string sceneToChangeTo;
public AudioSource audio;
// Start is called before the first frame update
void Start()
{
diolougeCurrentlyOn = 0;
audio.Play();
talkArea.text = diolouge[diolougeCurrentlyOn];
nameArea.text = name[diolougeCurrentlyOn];
if(isPlayer[diolougeCurrentlyOn]){
characterOneRoot.SetActive(true);
characterTwoRoot.SetActive(false);
characterOne.sprite = icon[diolougeCurrentlyOn];
}else{
characterOneRoot.SetActive(false);
characterTwoRoot.SetActive(true);
characterTwo.sprite = icon[diolougeCurrentlyOn];
}
}
public void NextTalk(){
if(diolougeCurrentlyOn >= (diolouge.Length - 1)){
SceneManager.LoadScene(sceneToChangeTo, LoadSceneMode.Single);
}else{
diolougeCurrentlyOn++;
audio.Play();
talkArea.text = diolouge[diolougeCurrentlyOn];
nameArea.text = name[diolougeCurrentlyOn];
if(isPlayer[diolougeCurrentlyOn]){
characterOneRoot.SetActive(true);
characterTwoRoot.SetActive(true);
characterOne.sprite = icon[diolougeCurrentlyOn];
}else{
characterOneRoot.SetActive(true);
characterTwoRoot.SetActive(true);
characterTwo.sprite = icon[diolougeCurrentlyOn];
}
}
}
// Update is called once per frame
void Update()
{
if (Input.GetMouseButtonDown(0) || Input.GetKeyDown(KeyCode.Return))
{
NextTalk();
}
}
}
Answer by Llama_w_2Ls · Jun 14, 2021 at 07:48 PM
Use an object that stores all these fields, for example:
[System.Serializable]
public class Dialogue
{
public string diolouge;
public string name;
public Sprite icon;
public bool isPlayer;
}
Then, instead of having to create arrays of fields:
public string[] diolouge;
public string[] name;
public Sprite[] icon;
public bool[] isPlayer;
Just use a:
public Dialogue[] Dialogues;
so I tried this and messed around with it but now its at a point where its wanting object references for: defaultIcon, Dialogue.diolouge, Dialogue.name, Dialogue.isPlayer, Dialogue.icon, my new code:
public class DiolougeManager : MonoBehaviour
{
[System.Serializable]
public class Dialogue
{
public string diolouge;
public string name;
public Sprite icon;
public bool isPlayer;
public Dialogue(){
diolouge = "hello";
name = "$$anonymous$$otale:";
icon = defaultIcon;
isPlayer = true;
}
}
public Dialogue[] Dialogues;
public Sprite defaultIcon;
public Text talkArea;
public Text nameArea;
public Image characterOne;
public GameObject characterOneRoot;
public Image characterTwo;
public GameObject characterTwoRoot;
public int diolougeCurrentlyOn;
public string sceneToChangeTo;
public AudioSource audio;
// Start is called before the first frame update
void Start()
{
diolougeCurrentlyOn = 0;
audio.Play();
talkArea.text = Dialogue.diolouge[diolougeCurrentlyOn];
nameArea.text = Dialogue.name[diolougeCurrentlyOn];
if(Dialogue.isPlayer[diolougeCurrentlyOn]){
characterOneRoot.SetActive(true);
characterTwoRoot.SetActive(false);
characterOne.sprite = Dialogue.icon[diolougeCurrentlyOn];
}else{
characterOneRoot.SetActive(false);
characterTwoRoot.SetActive(true);
characterTwo.sprite = Dialogue.icon[diolougeCurrentlyOn];
}
}
public void NextTalk(){
if(diolougeCurrentlyOn >= (Dialogues.Length - 1)){
SceneManager.LoadScene(sceneToChangeTo, LoadSceneMode.Single);
}else{
diolougeCurrentlyOn++;
audio.Play();
talkArea.text = Dialogue.diolouge[diolougeCurrentlyOn];
nameArea.text = Dialogue.name[diolougeCurrentlyOn];
if(Dialogue.isPlayer[diolougeCurrentlyOn]){
characterOneRoot.SetActive(true);
characterTwoRoot.SetActive(true);
characterOne.sprite = Dialogue.icon[diolougeCurrentlyOn];
}else{
characterOneRoot.SetActive(true);
characterTwoRoot.SetActive(true);
characterTwo.sprite = Dialogue.icon[diolougeCurrentlyOn];
}
}
}
// Update is called once per frame
void Update()
{
if (Input.GetMouseButtonDown(0) || Input.GetKeyDown(KeyCode.Return))
{
NextTalk();
}
}
}
Here's how you would use the array of Dialogues. Instead of: talkArea.text = Dialogue.diolouge[diolougeCurrentlyOn]
it should be:
talkArea.text = Dialogues[diolougeCurrentlyOn].diolouge;
Same for name, icon and other fields.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
How can i create a conversion like whatsapp in unity3d 0 Answers
Visual Novel Events 0 Answers
Is there a way to simplify this code? 2 Answers