- Home /
Question by
Kattlelack · Jul 18, 2014 at 02:27 AM ·
arraysystemindex
System.Array.IndexOf
I am making a combat log and here is my code:
using UnityEngine;
using System.Collections;
public class Log : MonoBehaviour {
public string[] lines;
int firstEmpty;
void Start () {
}
// Update is called once per frame
void Update () {
firstEmpty = System.Array.IndexOf(lines, null);
}
void OnGUI(){
GUI.Box (new Rect(5, Screen.height- 200 , 300, 200), "Log");
for(int line = 0; line < lines.Length; line++){
GUI.Label(new Rect(7, (Screen.height- 200 + 175) - (20 * line), 200, 20), lines[line]);
}
}
public void newLine(string log){
firstEmpty = System.Array.IndexOf(lines, null);
lines[firstEmpty + 1] = log;
}
}
It only displays one label and i want it to display all 9 lines. Thanks for the help.
Comment
Best Answer
Answer by smallbit · Jul 18, 2014 at 02:52 AM
Frist set the size of newLine to 9 in editor, than modify a little bit the method newLine
public void newLine(string log)
{
firstEmpty = System.Array.IndexOf(lines,"");
lines[firstEmpty] = log;
}
If you want the reverse order you need to modify that line :
GUI.Label(new Rect(7, (Screen.height- 200 + 175) - (20 * line), 200, 20), lines[line]);
To
GUI.Label(new Rect(7, (Screen.height - 200 ) + (20 * line), 200, 20), lines[line]);
Than you would need to update the list when number of messages reach 9. Remove the top most, move each consecutive to the previous place, and append the new one in the end. Well there is many ways of doing that. The code for now will throw IndexOutOfRangeException: Array index is out of range. at message number 10.