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 /
  • Help Room /
avatar image
0
Question by wcoltd · Jun 03, 2017 at 04:24 PM · textcoroutineclear

[Solved] computer auto-typing and clearing text field for next message

So I have a program that types a text adventure, it types the text character by character. The problem is, the text does not clear when going to the next message, the previous message in the text field is still there when the coroutine types the next message. I have tried everything I could think of to solve the problem but there is no difference, no matter what I do.

Expected Result: I want the text field to clear before the coroutine TypeText() types on the line.

Result: The text is left over from the previous message, and the new message is written in over one letter at a time, and then the rest of the previous message gets deleted one character at a time:

What I have tried: I have tried emptying the text field by setting textComp = ""; before the next message;

I have tried emptying the string message by setting message to "";

I tried changing the message into a message array so it doesn't reference the same string;

I tried stopping the coroutine right before starting it back up again.

None of these methods changed the result at all. I have run out of ideas of what to do to fix this.

 using System;
 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.UI;
 
 public class TextController : MonoBehaviour {
 
     Coroutine lastTypeRoutine = null;
 
     private enum States {intro, cell, mattress_0, mirror_0, cell_mirror, matress_1, lock_1, freedom};
     
 private States myState;
 
     string message="";
 
     Text textComp;
 
     // Use this for initialization
     void Start()
     {
         myState = States.intro;
         textComp = GetComponent<Text>();
         state_intro();
 
     }
 
 
     void Update()
     {
         if(myState == States.intro)
         {
             state_intro();
             if (Input.GetKeyDown(KeyCode.Return))
             {
                 myState = States.cell;
             }
         }
         if(myState == States.cell)
         {
             state_cell();
         }
         if(myState == States.mattress_0)
         {
             state_mattressLook();
         }
     }
     void state_intro()
     {
         message = "Welcome to prison! hit ENTER to continue!";
         textComp.text = "";
         lastTypeRoutine = StartCoroutine(TypeText());
     }
     void state_cell()
     {
         message = "You are trapped inside Fulsome Prison for a crime you didn't commit, your rival is seeking to have you killed within the confines of the prison walls. "
                     + "You must find a way to escape your cell. Inside your cell is an old mattress, a broken mirror. The cell gate is locked from the outside. \n\n"
                     + "Press RIGHT ARROW for Mattress, \n"
                     + "Press FORWARD ARROW for Broken Mirror, \n"
                     + "Press RIGHT ARROW for Cell Gate Lock."
                     ;
         textComp.text = "";
         lastTypeRoutine = StartCoroutine(TypeText());
         if (Input.GetKeyDown(KeyCode.RightArrow))
         {
             myState = States.mattress_0;
         }
     }
     void state_mattressLook() {
         message = "The mattess is worn and disgusting, stains and holes in it, they make you sleep on it without any sheets, there's a hole in the mattress.\n"
                       + "Press UP ARROW to stick your hand in the mattress hole. \n"
                       + "Press DOWN ARROW to return to where you were in your cell.";
         textComp.text = "";
         lastTypeRoutine = StartCoroutine(TypeText());
         if (Input.GetKeyDown(KeyCode.DownArrow))
         {
             myState = States.cell;
         }
             }
    
     
      IEnumerator TypeText()
         {
             foreach (char letter in message.ToCharArray())
             {
                 textComp.text += letter;
                 yield return 0;
             }
       
         }
    
    
 }


[SOLVED] I solved this by changing the code in the coroutine to clear the text field when a certain key was pressed. Notice the if statement that sets i = 0, and the clearing of the message box to "". Doing this ensured the box was empty on the key press. I added a few lines of code at the end so that I could hit enter and have the whole text appear.

     IEnumerator ShowText()
     {
       
         if (Input.GetKeyDown(KeyCode.Return) && firstReturn == true || Input.GetKey(KeyCode.RightArrow) && firstReturn == true || Input.GetKey(KeyCode.LeftArrow) && firstReturn == true || Input.GetKey(KeyCode.RightArrow) && firstReturn == true || Input.GetKey(KeyCode.DownArrow)&& firstReturn == true)
         {   
             i = 0;
             message = "";
             keyTimer = 0;
             firstReturn = false;
             print("firstReturn:" + firstReturn);
         }
         if (isTyped == false && firstReturn == false)
         {
             for (i=0; i < message.Length; i++)
             {
                 currentText = message.Substring(0, i);
                 this.GetComponent<Text>().text = currentText;
                 if (Input.GetKeyDown(KeyCode.Return))
                 {
                     i = message.Length - 1;
                     currentText = message.Substring(0, i);
                     this.GetComponent<Text>().text = currentText;
                 }
                 if (i >= (message.Length - 1))
                 {
                     isTyped = true;
                 }
                 yield return 0;
 
             }
         }
     }


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

Answer by Chinmay_Gawande · Jun 19, 2017 at 06:15 PM

Mate I have Same problem but did'nt Understood the code u typed can you please write it in simple manner

Comment
Add comment · 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

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

112 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

Related Questions

Issues with Scrolling Dialogue (repeating characters when printing text) 0 Answers

Coroutine does not stop? 1 Answer

How to change the text inside UI>Text 1 Answer

Unity 5 - Does anyone know how to scale the text in the UI? 1 Answer

CachedTextGenerator no longer has any character info in Unity 5.3 0 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