• 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
Question by ToxicNova · Mar 19, 2015 at 04:29 PM · javascriptuiscript.textunity 4.6

I'm trying to display my ammo; not working

Hello, I've been using this script to shoot bullets out of my gun, and decided to add ammo and all that to the script. After i did that, I tried to display my ammo but it didn't do anything to the text. Here is the script:

 #pragma strict
 import UnityEngine.UI;
   
 var bullet : GameObject;
 private var bulletsPerSecond = 20.0;
 private var shooting = false;
 private var Allammo : int = 320;
 private var ammo : int = 35;
 var sound: AudioClip; 
 var Gun : GameObject;
 var ammoDisplay : Text;

 function Start()
 {
     InvokeRepeating("Shoot", 0.0, 1.0 / bulletsPerSecond);
     ammoDisplay = GetComponent.<Text>(); //
 }

 function Shoot()
 {
     if (!shooting) return;
     var go = Instantiate(bullet, transform.position, transform.rotation);
     go.rigidbody.AddRelativeForce(Vector3.forward * 1000.0);
     ammo = (ammo - 1);
     audio.PlayOneShot(sound);
 }

 function Update()
 {
     shooting = false;
     if(Input.GetAxis("Fire1"))
     {
         shooting = true;
     }
     if (ammo == 0)
     {
         Allammo = (Allammo - 35);
     }
     ammoDisplay.text = ammo + "/" + Allammo.ToString();    
 }

Thanks in advance

Comment

People who like this

0 Show 6
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 hav_ngs_ru · Mar 19, 2015 at 05:05 PM 0
Share

is it compiled well? no errors?

 ammoDisplay = GetComponent.<Text>(); //

is this really correct for JS?

avatar image ToxicNova · Mar 19, 2015 at 05:27 PM 0
Share

I get no errors so I guess that is correct

avatar image hexagonius · Mar 19, 2015 at 06:15 PM 0
Share

That GetComponent.<...is not correct, therefore should throw an error. If it doesn't mono has probably lost connection to unity, which could mean your changes never made it into the game. Resync mono I'd suggest

avatar image tanoshimi · Mar 19, 2015 at 07:25 PM 0
Share

@hexagonius @hav_ngs_ru There's nothing wrong with the syntax. http://docs.unity3d.com/Manual/GenericFunctions.html

@toxicnova "didn't do anything to the text" means what exactly? Do you just see 35/320?

avatar image ToxicNova · Mar 19, 2015 at 09:25 PM 0
Share

The default text when i created a new text was "New Text" When I made this script and played the game, the text still read "New Text" not "35/320" which is what I want it to say. I want the 32 to decrease every time i shoot and display that on the screen

Show more comments

2 Replies

· Add your reply
  • Sort: 
avatar image
Best Answer

Answer by ToxicNova · Apr 11, 2015 at 09:27 PM

So I finally got a script to work. I gave up on it a month ago, but decided to experiment. I got this as my final script and it works.

 #pragma strict
   
 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;    
 }


I actually added this is two scripts, one to a text field on a canvas, and one to the gun. This is the script on the text. It differs from the one on the gun but are very alike.

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

 

 

Comment

People who like this

0 Show 1 · 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 14, 2015 at 05:42 PM 0
Share

note that the whole function shoot part is pretty much not needed, and doesn't affect anything if you leave it the way it is. The only thing required under the shoot funtion on the text script is ammo = ammo - 1

avatar image

Answer by UNZoOM · Mar 19, 2015 at 07:14 PM

 Line 16. ammoDisplay = GetComponent.<Text>(); // ERROR 
 
 C# :
  ammoDisplay = GetComponent.<Text>();
 
 Js :
  
  ammoDisplay = GetComponent (Text);

Comment

People who like this

0 Show 12 · 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 · Mar 19, 2015 at 09:29 PM 0
Share

I changed line 16, though it still displays "New Text"

avatar image UNZoOM · Mar 20, 2015 at 02:50 PM 0
Share

Alright , my major concern was getComponent , can you create a string and assign that string to the ammoDisplay textbox and see if it updates ?

Also , since ammo is an int , shouldn't this
ammoDisplay.text = ammo + "/" + Allammo.ToString();
be ammoDisplay.text = ammo.ToString() + "/" + Allammo.ToString();

avatar image ToxicNova · Mar 20, 2015 at 07:34 PM 0
Share

Do i need to use a GUI text, because it still isn't working. The only problem is when i add the component "GUI Text" to a "Text", it doesn't appear. I changed the font size and everything, and i gave it some text but the GUI text doesn't show up

avatar image UNZoOM · Mar 20, 2015 at 07:43 PM 0
Share

Are you viewing the GUI text in the Game view or Scene View ? Also which version of Unity are you using ?

avatar image ToxicNova · Mar 20, 2015 at 08:07 PM 0
Share

I can't view the GUI text in the game or scene view, but I'm trying to view it in game mode. Also I'm using Unity 4.6.1f1

Show more comments

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

23 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 avatar image avatar image avatar image avatar image avatar image

Related Questions

How to render html text in new UI system (unity 4.6) 0 Answers

Pause Menu Issue (Black Screen) 0 Answers

Text wont change to correct value 1 Answer

How To Change Color Of Text On UI When It's Selected | Unity 4.6 3 Answers

Unity 4.6 Text Misbehaves During Gameplay 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