- Home /
Question by
StarlingSoftworksInteractive · Feb 13, 2017 at 12:13 PM ·
c#unity 5unity5scripting beginner
How would you clear a string or previous text?
Hey guys, How would clear all the text? I don't really know how to do it
public Canvas myCanvas;
public Text myText;
// Creates varibles to view more text
private string display = "";
private bool engagedText;
List<string> textEvents;
// Creates varibles inorder to reconised input commands
public InputField inputfield;
// Creates a Command string that holds commands
private Dictionary<string, System.Action<string,string>> commands;
protected void Awake()
{
//Holds the command string and points them to their methods and functions
// Listen when the inputfield is validated
commands = new Dictionary<string, System.Action<string,string>>();
commands.Add( "help", onHelpTyped );
inputfield.onEndEdit.AddListener( OnEndEdit );
}
void Start () {
textEvents = new List<string>();
textEvents.Add ("Welcome to my App");
textEvents.Add ("Your currently offline (console), login to go online");
textEvents.Add ("Type help for help");
engagedText = true;
}
void Update () {
if(engagedText)
{
AddText();
engagedText = false;
}
}
private void OnEndEdit( string input )
{
// Only consider onEndEdit if Submit (Enter/return) key being press
if ( !Input.GetButtonDown( "Submit" ) )
return;
bool commandFound = false;
// If the command is entered,it finds the correct command
foreach ( var item in commands )
{
if ( item.Key.ToLower().StartsWith( input.ToLower() ) )
{
commandFound = true;
item.Value( item.Key, input );
break;
}
}
// If command is not founded, perform this task & clear the InputField
if ( !commandFound )
textEvents.Add ("Command not founded");
engagedText = true;
inputfield.text = "";
}
// Help function
private void onHelpTyped( string command, string input )
{
textEvents.Add ("hi");
textEvents.Add("hello");
textEvents.Add("mate");
engagedText = true;
}
// Add the new text while keeping the old text.
void AddText()
{
display = "";
foreach(string msg in textEvents)
{
display = display.ToString () + msg.ToString() + "\n";
}
myText.text = display;
}
}
Comment
Which text are you referring to? Could you be a bit more specific?
these text I mean, so basically this text showed up at the start of the game and when I type in help the next set of text shows up and now I am trying to clear all the text when the user type in "clear"
textEvents.Add ("Welcome to my App");
textEvents.Add ("Your currently offline (console), login to go online");
textEvents.Add ("Type help for help");
private void onHelpTyped( string command, string input )
{
textEvents.Add ("hi");
textEvents.Add("hello");
textEvents.Add("mate");
engagedText = true;
}
Best Answer
Answer by Chubzdoomer · Feb 13, 2017 at 09:34 PM
If you want to clear all of the text, you can add this line below "commands.Add("help", onHelpTyped);":
commands.Add("clear", onClearTyped);
Then create a new onClearTyped function that is as follows:
private void onClearTyped (string command, string input)
{
textEvents.Clear();
}
Thanks for your help mate. I reallyappreciate it
Answer by tanoshimi · Feb 13, 2017 at 09:27 PM
textEvents is a list. So, if you want to clear it:
textEvents.Clear();
Your answer
![](https://koobas.hobune.stream/wayback/20220612110958im_/https://answers.unity.com/themes/thub/images/avi.jpg)