- Home /
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
is it compiled well? no errors?
ammoDisplay = GetComponent.<Text>(); //
is this really correct for JS?
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
@hexagonius @hav_ngs_ru There's nothing wrong with the syntax. http://docs.unity3d.com/$$anonymous$$anual/GenericFunctions.html
@toxicnova "didn't do anything to the text" means what exactly? Do you just see 35/320?
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
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);
}
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
Answer by UNZoOM · Mar 19, 2015 at 07:14 PM
Line 16. ammoDisplay = GetComponent.<Text>(); // ERROR
C# :
ammoDisplay = GetComponent.<Text>();
Js :
ammoDisplay = GetComponent (Text);
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();
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
Are you viewing the GUI text in the Game view or Scene View ? Also which version of Unity are you using ?
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
Your answer
Follow this Question
Related Questions
TextField text component not updating 2 Answers
RPG Text 2D on Object Interaction Javascript 0 Answers
Based on text content increase container size 1 Answer
Variable won't appear on UI text (JS) 0 Answers
sending a variable to a text object 1 Answer