Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • 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
0
Question by Codelyy · Mar 06, 2016 at 03:28 AM · c#textfunctiontextbox

One Text box after another problem

I have already made another topic about this but I wanted to add on some more information about what I've done.

Basically I'm wanting to create an NPC conversation with one text box appearing after another with text in when the user presses a button which currently is "Z". I've done all the scripting for the text and made it so it prints one letter at a time also but I just can't think of a way to have one text box after another and I think that's mainly due to the way I've scripting the text manager.

This is the code within my text manager:

 using UnityEngine;
 using System.Collections;
 using UnityEngine.UI;
 
 public class TextManager : MonoBehaviour {
     public GameObject textbubble;
     public Text textbox;
 
     public GameObject NPC;
 
     public bool isTyping;
     public bool finishedText;
 
     void Start () {        
         NPC = GameObject.Find ("NPC");
     }
         
     //To Input Text and Animation for NPC
     public void InputText(string text, Sprite sprite, float typeSpeed){
         
         StartCoroutine (TypeScroll (text, typeSpeed));
         NPC.GetComponent<SpriteRenderer> ().sprite = sprite;
     }
 
     public IEnumerator TypeScroll(string lineOfText, float typeSpeed){
         int letter = 0;
         textbox.text = "";
         isTyping = true;
         finishedText = false;
 
         while(isTyping && letter < lineOfText.Length) {
             textbox.text += lineOfText [letter];
             letter++;
             yield return new WaitForSeconds (typeSpeed);
 
             if (letter == lineOfText.Length) {
                 isTyping = false;
                 finishedText = true;
 
             } else {
                 if (Input.GetKey (KeyCode.Z)) {
                     textbox.text = lineOfText;
                     isTyping = false;
                     finishedText = true;
                 }
             }
         }
     }
 }

All it does is basically take in a string, a sprite of the NPC and then the speed of which it will print out the letter and then runs it all in a IEnumerator. This is how i call the function:

 textManager.InputText ("Calling the function", sprites [0], 0.1f);    

I did this so i could easily change the sprite of the NPC for depending on what he's saying or even slow or speed up the type speed depending on what he's saying.

Someone on the last topic i made about this mentioned using a array but the problem with that, that i didn't mention in the last post is I can't easily change the sprite or type speed then as it would keep looping through the same line of code while adding one to the index of the array to print out each string in that array one after the other.

So basically I'm wondering if anyone has I way i can print one message after another if the user presses a button that works with my function?

Thanks

Comment
Add comment
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
0
Best Answer

Answer by flashframe · Mar 06, 2016 at 07:14 AM

One way to do it is to make a little class that holds the three things you want to pass to your InputText function: Text, Sprite, Speed. Then you can make an array/list of this class to hold all the different lines you want to display, and loop through that.

Example of Class to hold data

 public class DialogueLine
 {
     public string text;
     public Sprite sprite;
     public float typeSpeed;
 
     public DialogueLine(string _text, Sprite _sprite, float _speed)
     {
         text = _text;
         sprite = _sprite;
         typeSpeed = _speed;
     }
 }

Example of how to create a Dialogue Line and add it to a list

 public List<DialogueLine> lines;
 lines.Add(new DialogueLine("Text", OldManSprite, 5f));

Example of how to use it in your script

 public List<DialogueLine> lines;
     int currentLine = 0;
     ...
         void PlayNextLine()
         {
             InputText(lines[currentLine].text, lines[currentLine].sprite, lines[currentLine].typespeed);
             currentLine ++;
         }
 
 void Update()
 {
     if (Input.GetKeyDown (KeyCode.Z)) {
         PlayNextLine();
     }
 }




Comment
Add comment · 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 Codelyy · Mar 06, 2016 at 03:57 PM 0
Share

Brilliant!, Thanks.

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.

Follow this Question

Answers Answers and Comments

114 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

Related Questions

How do I call an DontDestroyOnLoad function on UI text 2 Answers

Unity 2D - Text UI and Textbox scripting issue - HELP ASAP! 2 Answers

One text box after another 2 Answers

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers


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