- Home /
Transfer Data Between Two Loaded Scenes
I have 2 scenes, my main game scene and a dialogue UI scene.
When tested in just one scene (the UI Scene with a button), it all worked fine.
However i want to transfer data from the main game scene to my dialogue scene.
I have 3 Scripts: 1) Dialogue Manager
public class DialogueManager : MonoBehaviour {
public TextMeshProUGUI nameText;
public TextMeshProUGUI dialogueText;
public float sentenceSpeed;
public Image faceBox;
public Animator animator;
private Queue<string> sentences;
void Start()
{
sentences = new Queue<string>();
}
public void StartDialogue(Dialogue dialogue)
{
animator.SetBool("isOpen", true);
nameText.text = dialogue.name;
faceBox.sprite = dialogue.face;
sentenceSpeed = dialogue.sentenceSpeed;
sentences.Clear();
foreach(string sentence in dialogue.sentences)
{
sentences.Enqueue(sentence);
}
DisplayNextSentence();
}
public void DisplayNextSentence()
{
if (sentences.Count == 0)
{
EndDialogue();
return;
}
string sentence = sentences.Dequeue();
StopAllCoroutines();
StartCoroutine(TypeSentence(sentence));
}
IEnumerator TypeSentence(string sentence)
{
dialogueText.text = "";
foreach (char letter in sentence.ToCharArray())
{
dialogueText.text += letter;
yield return new WaitForSeconds(sentenceSpeed);
}
}
void EndDialogue()
{
animator.SetBool("isOpen", false);
}
}
2) Dialogue
[System.Serializable] public class Dialogue { public string name;
public Sprite face;
public float sentenceSpeed = 0.5f;
[TextArea(3, 20)]
public string[] sentences;
}
3) Dialogue Trigger
public class DialogueTrigger : MonoBehaviour { public Dialogue dialogue;
public void TriggerDialogue()
{
FindObjectOfType<DialogueManager>().StartDialogue(dialogue);
}
} When tested with the button you would attach the Dialogue Trigger to the button and fill out the data, when you hit the button it would pass the dialogue to the dialogue manager and display it on the screen.
Now i want the Dialogue Trigger on an object and when you enter its collider and press a key (i've got this working) pass the data from the main game scene to the dialogue manager on the dialogue scene.
The dialogue scene has been loaded using LoadSceneMode.Additive.