- Home /
Conversation Bubbles and starting it
Hello I'm trying to make a 2D rpg game and I did some coding on it to make the dialogues/boxes come out from the screen(animation) and you can click on a button to just keep reading the dialogues. And when it finishes goes out, the thing is that I need to click a button to start the dialogue, how can I make it start with a simple click or just whenever you load in the scene just starts the dialogue?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using TMPro;
public class DialogueManager : MonoBehaviour
{
public TextMeshProUGUI speakerName, dialogue, naviButtonText;
public Image speakerSprite;
private int currentIndex;
private Conversation currentConvo;
private static DialogueManager instance;
private Animator anim;
private Coroutine typing;
private void Awake()
{
if(instance == null)
{
instance = this;
anim = GetComponent<Animator>();
}
else
{
Destroy(gameObject);
}
}
public static void StartConversation(Conversation convo)
{
instance.anim.SetBool("isOpen", true);
instance.currentIndex = 0;
instance.currentConvo = convo;
instance.speakerName.text = "";
instance.dialogue.text = "";
instance.naviButtonText.text = "";
instance.ReadNext();
}
public void ReadNext()
{
if(currentIndex > currentConvo.GetLength())
{
instance.anim.SetBool("isOpen", false);
return;
}
speakerName.text = currentConvo.GetLineByIndex(currentIndex).speaker.GetName();
if(typing == null)
{
typing = instance.StartCoroutine(TypeText(currentConvo.GetLineByIndex(currentIndex).dialogue));
}
else
{
instance.StopCoroutine(typing);
typing = null;
typing = instance.StartCoroutine(TypeText(currentConvo.GetLineByIndex(currentIndex).dialogue));
}
speakerSprite.sprite = currentConvo.GetLineByIndex(currentIndex).speaker.GetSprite();
currentIndex++;
if(currentIndex >= currentConvo.GetLength() + 1)
{
naviButtonText.text = "";
}
}
private IEnumerator TypeText(string text)
{
dialogue.text = "";
bool complete = false;
int index = 0;
while (!complete)
{
dialogue.text += text[index];
index++;
yield return new WaitForSeconds(0.02f);
if(index == text.Length)
{
complete = true;
}
}
typing = null;
}
}
And this is the code for the test button
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
public class Tester : MonoBehaviour
{
Button buttonToHide;
public Conversation convo;
void Start()
{
Invoke("OpenDialogue", 5);
buttonToHide = GetComponent<Button>();
buttonToHide.onClick.AddListener(() => HideButton());
}
public void StartConvo()
{
DialogueManager.StartConversation(convo);
}
void HideButton()
{
buttonToHide.gameObject.SetActive(false);
}
}
Thanks!
Answer by mf41z · Jan 13 at 11:58 AM
So, when you press a button, the dialogue starts. And you want to make that dialogue start simply by mouse clicking or when the scene starts? Get the listener you added to the button, and just trigger it through Start() or do a if(Input.GetMouseButtonDown(0)) action();
under Update() method
void Start()
{
Invoke("OpenDialogue", 5);
DialogueManager.StartConversation(convo);
buttonToHide = GetComponent<Button>();
buttonToHide.onClick.AddListener(() => HideButton());
}
or
Update()
{
if(Input.GetMouseButton(0))
{
DialogueManager.StartConversation(convo);
}
}
Your answer
Follow this Question
Related Questions
Cant Get Dialogue Trigger Working 0 Answers
Make a button selectable for controller but not interactable 0 Answers
How to disable button animation fade? 1 Answer
Rotating object with onscreen button 1 Answer
How to change button text via script? 2 Answers