- Home /
Problem with writing text in a box - typewriter style
I'm trying to do writing text in a box in a typewriter fashion for my lil' 3d platformer project (for cutscenes), but for some reason it doesn't show up. Not even empty Box:
public string text = "";
public int delayInFrames = 5;
private string tmptext = "";
private bool showText = true;//false;
private int textCounter = 0;
public float boxX;
public float boxY;
public float boxWidthInPixels;
public float boxHeightInPixels;
// Use this for initialization
void Start () {
}
void OnGUI() {
if (showText) {
if (/*(text!="") &&*/ (tmptext.Length<text.Length)) {
if (Mathf.Round(Time.time % delayInFrames) <1){
textCounter++;
tmptext = text.Substring(0,textCounter);
}
}
GUI.Box(new Rect(boxX,boxY,boxWidthInPixels,boxHeightInPixels),tmptext);
}
}
// Update is called once per frame
void Update () {
if (showText){
if (Input.GetButtonUp("Action")){
if (text!=tmptext){
textCounter = text.Length;
tmptext = text;
}
} else {
showText = false;
}
}
}
This is inside standard MonoBehavior. As you can see, it should show up right from the start, empty box at least (showText being true), but it shows literally nothing. Also box's x/y and width/height are inside screen boundaries, but it still doesn't seem right (or rather doesn't seem at all ;)). Can you help me?
Note that I am not interested in asset store packages as: 1) I don't have money for 'em. 2) I want to learn. 3) I am pretty sure I can achieve it on my own (save for some help) as I made similar effects in other languages.
Answer by Adamcbrz · Dec 18, 2012 at 04:58 AM
darkhog,
The problem is you are setting showText = false in the Update if Input.GetButtonUp("Action") doesn't equal true.
Right... How could I be so stupid? Next time I will, please hit me with 5-ton hammer.