- Home /
How Do I Display Multiple Strings In Here?
This script pretty much makes a Debug/Chat log but it only displays one string at a time. How do i get it to print the next string underneath rather than overwrite the previous one?
var stringToEdit : String = "";
function OnGUI() {
GUI.Label(Rect(10, Screen.height / 4*3.5, Screen.width / 3, Screen.height / 10), stringToEdit, "textarea");
}
Answer by Dark_Tiger · Oct 23, 2014 at 06:33 AM
Increment the Y value after you write each string
var stringToEdit : String = "";
int y = Screen.height / 4*3.5;
function OnGUI()
{
GUI.Label(Rect(10, y, Screen.width / 3, Screen.height / 10), stringToEdit, "textarea");
y += Screen.height / 4*3.5;
}
I'm a bit new to this, so I'm guessing that's where the y value is passed in for the label.
I'm not sure what that means. This is set up so, rather than printing to the editor console, it displays the text in to onscreen box.
On another script that uses this:
stringToEdit.stringToEdit = ("Picked up SpaceRocks");
I'm not sure if what I did was right, I'm really a noob at this xD but that's the sort of logic you need to add, so you're increasing the y value each time you go through, that will display the message lower and lower.
Yeah I could see your logic but I'm not sure if that would be the solution at this time. What I'm really looking for is a multi-lined text field that I can't edit or select. All it needs to do is display text sent to it and display the next lot of text sent to it on the next line, if that makes sense..
I really don't know :L the guys below seem to know their stuff more
Answer by fafase · Oct 23, 2014 at 06:59 AM
var offsetY:float = 50f;
function OnGUI() {
for(var i:int = 0; i < amountOfString; i++){
GUI.Label(Rect(10, (Screen.height / 4*3.5) + offsetY * i, Screen.width / 3, Screen.height / 10), stringToEdit, "textarea");
}
}
offsetY is how much space you want between each writing. Then the y value of the printing gets the original place plus this offset times the i value.
So the first will get
(Screen.height / 4*3.5) + offsetY * i -> i is 0
(Screen.height / 4*3.5) + offsetY * i -> i is 1 so 50
(Screen.height / 4*3.5) + offsetY * i -> i is 2 so 100
and so on
Answer by robertbu · Oct 23, 2014 at 07:08 AM
As an alternate to placing multiple labels (which will work), you can take your collection of strings and build a single string that has a newline between each string.
#pragma strict
private var stringToEdit : String = "";
private var chatLines : String[] = ["One", "Two", "Three", "Four", "Five", "Six"];
function Start() {
BuildText();
}
function OnGUI() {
GUI.Label(Rect(10, Screen.height / 4*3.5, Screen.width / 3, Screen.height / 10), stringToEdit, "textarea");
}
function BuildText() {
stringToEdit = "";
for (var i = 0; i < chatLines.Length; i++) {
stringToEdit += chatLines[i] + "\n";
}
}
In this bit of sample code, chatLines would be the incoming chat lines. You would be updating this list each time a new line comes in and then calling BuildText. Attach this sample code to an empty game object and hit run.
I would say you have your answer in either this one or $$anonymous$$e. The goal would define which suits best. $$anonymous$$ine, I'd think, would be better for distinct and separate information that you want to space out. On the other end if it is a long sentence then you may go better with this one which will keep things closer together. On a side note on this one above, if your string gets to be real long, you may want to use a StringBuilder ins$$anonymous$$d if you were to do that often.
Your answer
Follow this Question
Related Questions
Simulating a Log 2 Answers
How do I change the logged text based on a momentary collision? 1 Answer
Debug.Log print order, or Awake vs Start issue? 1 Answer
Standalone save output_log 2 Answers
Problems with simple dialogue 0 Answers