- Home /
Dynamic Scrolling Text for a combat log
I realized today that I Debug.Log a lot of stuff, and I'd rather build a text box where I add these new lines of text instead. I'm trying to build a box (canvas/panel I'm not sure which one I'm supposed to use) at the top right corner of the screen. In this box I want to be able to add a new line of text every time the player hits an enemy, picks up an item, reads a book, etc. The game I'm making is a roguelike with very minimal graphic, thus a log like this is incredibly important.
if(playerHitsEnemy == true)
{
textBoxController.AddNewLine("Player hit the enemy");
}
And after adding that new line I want it automatically jump to the bottom of the text log where the new line is. I figured for the new line function I would do something like this:
AddNewLine(string lineToAdd)
{
textBox.text += ("\n" + lineToAdd);
//Somehow jump to the bottom of the textBox
}
I've run into several problems in trying to build this. The first and biggest one is that the text never fits right. I'm using unity's premade "Scrollable View" UI object with a text object that it is parented to. I edit the text object's width and height fields so that it fits within the canvas. I don't touch its scale field.
The text object is where I'm adding all the text, but when my text has too many lines (When a new line falls outside of the scrollable view's canvas) I cannot scroll any further. If I have 10 lines of text but only 7 fit in the box, I cannot scroll down to see the other 3 lines. I've messed around with the height of the text object, and when the height is equal to the (The number of lines * Size of the text font) it fit right and I can scroll all the way to the bottom, however I don't know how to edit the text box's height in code and I feel that there MUST be a better solution.
My other problem is that I do not know how to automatically jump to the bottom of the text box, but this one is much less pressing.
I've looked up several tutorials but they're all out of date and haven't actually helped me. I'm using the latest version of Unity.
Please help!
Your answer
![](https://koobas.hobune.stream/wayback/20220612182718im_/https://answers.unity.com/themes/thub/images/avi.jpg)