So this is my script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
public class DialogManager : MonoBehaviour
{
public GameObject Battle;
public Text nameText;
public Text dialogText;
public Animator animator;
private Queue<string> sentences;
void Start()
{
GetComponent<Battle>;
sentences = new Queue<string>();
}
public void StartDialog(ObjectDialog dialog)
{
animator.SetBool("IsOpen", true);
nameText.text = dialog.name;
sentences.Clear();
foreach (string sentence in dialog.sentences)
{
sentences.Enqueue(sentence);
}
DisplayNextSentece();
}
public void DisplayNextSentece()
{
Battle.SetActive(true);
if (sentences.Count == 0)
{
EndDialog();
Battle.SetActive(true);
}
string sentence = sentences.Dequeue();
dialogText.text = sentence;
}
void EndDialog()
{
Debug.Log("End of conversation.");
animator.SetBool("IsOpen", false);
}
}
It depends what kind of game you are playing. In essence, there are two types.
- The first is where your player has to go through each level, rather like a puzzle game. Level 5 always follows level 4 and they go up sequentially.
- The second is where you are in a room with multiple doors. Each door takes you to a different level. They are not sequential and you can take any scene in any order, depending on the path you choose.
In either case, you have to go to File > Build Settings and add all the scenes that are in your game to the “Scenes in Build” panel. For the puzzle game, you put the levels in strict ascending order. As you do so, you will see that the number on the right hand side is the level number, starting at zero. For the free-flowing game, you can add the scenes in any order you wish but make sure that you know all the scene names and that there are no duplicates.
To change to the next sequential scene, you use SceneManager.GetActiveScene().buildIndex
to get the current scene number, store it in an Integer and increment it. Then use SceneManager.LoadScene(NextSceneNumber);
To change to a named scene you use: SceneManager.LoadScene("YourSceneName");
Not surprisingly, there is a lot more to it than that. You might use a Static class to manage all of this, rather than have it in every scene. You might use DontDestroyOnLoad to keep an object between scenes. You might want to preload a scene or have multiple scenes open. For all these and more, check the documentation.