• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by ToxicNova · Apr 11, 2015 at 09:33 PM · sizefontunity 4.6displayguibox

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 t$$anonymous$$ngs:

  • 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)

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image ToxicNova · Apr 11, 2015 at 09:36 PM 0
Share

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);
 }

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image ToxicNova · Apr 12, 2015 at 12:42 AM 0
Share

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.

avatar image ToxicNova · Apr 13, 2015 at 01:52 AM 0
Share

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 MaxDistance : 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.GetKeyDown(KeyCode.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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

18 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to increase font size of text displayed 2 Answers

How to find a font via script? 2 Answers

GUIskin global font size 0 Answers

Android OnGUI Font size problem. 0 Answers

multi line label with different font? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges