Changing a fontSize?

I am thinking that this doesn’t work because, the style I entered to the Label is local and doesn’t update…

using UnityEngine;
using System.Collections;
public class Menu : MonoBehaviour {
	void Start () {
		if (Screen.height < 1024) {
		if (Screen.width < 768) {
						Screen.SetResolution (1024, 768, true);
				}
			}
		}
	
	void Update () {

	}

	IEnumerator wait(float waiting)
	{
		yield return new WaitForSeconds(waiting);
		print ("Waited" + " " + waiting);
		StopCoroutine ("wait");
	}
	void OnGUI () {
		var height =  Screen.height;
		var width  =  Screen.width;
		int a = 3;
		GUIStyle desire = new GUIStyle();
		desire.fontSize  = a;
		desire.wordWrap = true;
		desire.fontStyle = FontStyle.Bold;
		GUI.contentColor = Color.white;
		GUI.Box (new Rect(0, 0, width, height), "");
		GUI.Label (new Rect(width/2, height/2, height, width/50), "Ripe games", desire);
		while (a < 31) {
			StartCoroutine (wait(1));
			a++;
			desire.fontSize = a;
		}
	}
 }

OnGUI is called every frame, and forgets any information it defined the previous frame. So what your code does is:

  • Every frame, the font size is assigned as 3
  • The the GUI displays “Ripe games” in size 3 font
  • Then it changes the font size up to 31
  • Then the frame ends
  • Then another frame begins, and font size is assigned as 3…

So you have a loop that’s changing the font size, but you never actually use the font size anywhere.

What you probably want instead is a Coroutine. Check the unity reference for more info. Within your coroutine, you want to wait a frame, then adjust the font size, then wait a frame… like this:

private int fontSize = 3;

IEnumerator changeFont()
{
    while (fontSize < 31)
    {
        fontSize++;
        yield return null;
    }
}