0
\$\begingroup\$

I've coded something in Unity using C# (see below) for a dialogue scene separate from the actual game. On the click of a button from the game, the player is taken to this dialogue scene where the dialogue starts playing. After the dialogue has finished playing, a new button, called "Return Button" appears and takes the player back to the game. However, I am having trouble with displaying the Return button. When I run this in my Unity project, it is able to go through all the dialogue correctly, but the Return button does not appear, thus the player is unable to return back to the game. How can I make it appear at the end of the dialogue?

This code is attached to an empty GameObject in the scene.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;
using UnityEngine.SceneManagement;

public class Dialogue : MonoBehaviour
{

    public TextMeshProUGUI textDisplay;
    public string[] sentences;
    private int index;
    public float typingSpeed;

    public GameObject continueButton;
    public GameObject endButton;

    void Start()
    {
        StartCoroutine(Type());
        endButton.SetActive(false);
    }

    void Update()
    {
        if (textDisplay.text == sentences[index])
        {
            continueButton.SetActive(true);
        }

        
        if (index == sentences.Length)
        {
            endButton.SetActive(true);
            SceneManager.LoadScene("Maze minigame");
        }
    }


    IEnumerator Type()
    {
        foreach(char letter in sentences[index].ToCharArray())
        {
            textDisplay.text += letter;
            yield return new WaitForSeconds(typingSpeed);
        }
    }

    public void NextSentence()
    {
        continueButton.SetActive(false);
        if(index < sentences.Length - 1)
        {

            index++;
            textDisplay.text = "";
            StartCoroutine(Type());
        }
        else
        {
            textDisplay.text = "";
            continueButton.SetActive(false);
        }
    }   
   
    public void Return()
    {
        SceneManager.LoadScene("Maze minigame");
    }
}

Any help will be much appreciated!!

Thank you in advance!

\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

From the content of NextSentence the value of the index will never be over sentences.Length - 1. you have to check that index is equal to sentences.Length - 1 not sentences.Length inside the Update method

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .