- Home /
How to change the font and size of a text in a gui box?
So I finally was able to get my ammo display to work, and was just wondering a few things:
How do I change the font of the text, because it seems to be only in Arial at the moment
How do I change the size of the text so it is bigger than the default size
How to make the box in the bottom, left area (not corner) of the screen
And finally, how to change the size of the box
Thanks in advance!
(Using Untiy 4.6 and javascript)
Oh yea, this is the script I was using to display my ammo:
#pragma strict
import UnityEngine.UI;
var bullet : GameObject;
private var bulletsPerSecond = 20.0;
private var shooting = false;
private var Allammo : int = 320;
var ammo : int = 40;
var sound: AudioClip;
var Gun : GameObject;
function Start()
{
InvokeRepeating("Shoot", 0.0, 1.0 / bulletsPerSecond);
}
function Shoot()
{
if (!shooting || ammo < 1) return;
var go = Instantiate(bullet, transform.position, transform.rotation);
go.rigidbody.AddRelativeForce(Vector3.forward * 1000.0);
ammo = (ammo - 1);
audio.PlayOneShot(sound);
}
function Update()
{
if ((Allammo > 0) || ((ammo <= 40) && (ammo > 0) && (Allammo == 0)))
{
if (ammo > 0)
{
if (Input.GetButtonDown("Fire1"))
{
shooting = true;
}
if (Input.GetButtonUp("Fire1"))
{
shooting = false;
}
}
else
{
shooting = false;
Allammo = (Allammo - 40);
ammo = 40;
}
}
else shooting = false;
}
function OnGUI ()
{
GUI.Box (Rect (10,10,100,90), ammo + "/" + Allammo);
}
Answer by ToxicNova · Apr 12, 2015 at 12:41 AM
Okay i just added:
var ChangeText : GUIStyle;
to is and it did the job. You can go to the inspector to change the font and size. As for where to place the text, I just looked at unity docs.
well you also have to do this:
GUI.Box (Rect (Screen.width - 1250,Screen.height - 50,100,50), ammo + "/" + Allammo, ChangeText);
To see it in the inspector.
This is my full script:
#pragma strict
import UnityEngine.UI;
var bullet : GameObject;
private var bulletsPerSecond = 20.0;
private var shooting = false;
private var Allammo : int = 320;
var ammo : int = 40;
var sound: AudioClip;
var Gun : GameObject;
var textStyle : GUIStyle;
var Reloadammo : int = 0;
var Distance : float;
var $$anonymous$$axDistance : int = 2;
var TAmmo : int = 1;
var Remainder : int = 0;
function Start()
{
InvokeRepeating("Shoot", 0.0, 1.0 / bulletsPerSecond);
}
function Shoot()
{
if (!shooting || ammo < 1) return;
var go = Instantiate(bullet, transform.position, transform.rotation);
go.rigidbody.AddRelativeForce(Vector3.forward * 1000.0);
ammo = (ammo - 1);
audio.PlayOneShot(sound);
}
function Update()
{
if ((Allammo > 0) || ((ammo <= 40) && (ammo > 0) && (Allammo == 0)))
{
if (ammo > 0)
{
if (Input.GetButtonDown("Fire1"))
{
shooting = true;
}
if (Input.GetButtonUp("Fire1"))
{
shooting = false;
}
}
else
{
shooting = false;
if (40 <= Allammo)
{
if (320 >= Allammo)
{
Allammo = (Allammo - 40);
ammo = 40;
}
}
if (40 > Allammo)
{
if (ammo != 40)
{
ammo = Allammo;
Allammo = 0;
}
}
}
}
else shooting = false;
if ((Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.R)) && (Allammo > 0) && (ammo < 40))
{
Reloadammo = (40 - ammo);
if (Allammo >= Reloadammo)
{
ammo = 40;
Allammo = (Allammo - Reloadammo);
}
}
}
function OnGUI ()
{
GUI.Box (Rect (Screen.width - 1250,Screen.height - 100,100,50), ammo + "/" + Allammo, textStyle);
}
Your answer

Follow this Question
Related Questions
How to increase font size of text displayed 2 Answers
Change Font Size of GUI Table 0 Answers
Font.GetCharacterInfo always returns false 3 Answers