- Home /
Question by
cenkbozkurt05 · Jun 08, 2021 at 03:32 PM ·
c#unity 2ddialogue
How can i change Scene when my dialogue is end
Hey guys, i'm making 2D game that will be dialogue at the beggining and i want to change scene when my dialogue end. but i dont know how to do that maybe this is a simple question but i'm pretty new in C#
CODE:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;
using UnityEngine.SceneManagement;
public class Dialogue : MonoBehaviour
{
public TextMeshProUGUI textDisplay;
[TextArea(3, 20)]
public string[] sentences;
private int index;
public float typingSpeed;
public GameObject continueButton;
private void Start()
{
StartCoroutine(Type());
}
private void Update()
{
if(textDisplay.text == sentences[index])
{
continueButton.SetActive(true);
}
if(WHAT SHOULD İ WOULD WRİTE HERE)
{
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex + 1);
}
}
IEnumerator Type()
{
foreach(char letter in sentences[index].ToCharArray())
{
if (Input.GetKey(KeyCode.J))
{
textDisplay.text += letter;
}
else
{
textDisplay.text += letter;
yield return new WaitForSeconds(0.04f);
}
}
}
public void NextSentences()
{
continueButton.SetActive(false);
if(index< sentences.Length -1)
{
index++;
textDisplay.text = "";
StartCoroutine(Type());
}
else
{
textDisplay.text = "";
continueButton.SetActive(false);
}
}
}
Comment
Best Answer
Answer by Hellium · Jun 08, 2021 at 03:42 PM
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;
using UnityEngine.SceneManagement;
public class Dialogue : MonoBehaviour
{
public TextMeshProUGUI textDisplay;
[TextArea(3, 20)]
public string[] sentences;
private int index;
public float typingSpeed;
public GameObject continueButton;
private void Start()
{
StartCoroutine(Type());
}
IEnumerator Type()
{
textDisplay.text = "";
foreach(char letter in sentences[index].ToCharArray())
{
if (Input.GetKey(KeyCode.J))
{
textDisplay.text += letter;
}
else
{
textDisplay.text += letter;
yield return new WaitForSeconds(0.04f);
}
}
continueButton.SetActive(true);
}
public void NextSentences()
{
if(index < sentences.Length - 1)
{
continueButton.SetActive(false);
index++;
StartCoroutine(Type());
}
else
{
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex + 1);
}
}
}