• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
Question by juancarlosgza · Jan 13, 2022 at 12:57 AM · buttonbuttonsnpcdialoguedialog

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 t$$anonymous$$ng 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 = t$$anonymous$$s;
             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;
 
         w$$anonymous$$le (!complete)
         {
             dialogue.text += text[index];
             index++;
             yield return new WaitForSeconds(0.02f);
 
             if(index == text.Length)
             {
                 complete = true;
             }
         }
 
         typing = null;
        
     }
 }


And t$$anonymous$$s 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!

Comment

People who like this

0 Show 0
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
Best Answer

Answer by mf41z · Jan 13, 2022 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);
         }
 }



Comment

People who like this

0 Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image juancarlosgza · Jan 17, 2022 at 01:36 PM 0
Share

Thank you!

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

156 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Cant Get Dialogue Trigger Working 0 Answers

How to make a No Ads Button 0 Answers

How do i add a delay to how often a unity ui button may be used? 1 Answer

Check if a listener has already been added to a button? 2 Answers

Looking for an invisible button (that actually works!) 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges