- Home /
Wierd GUI problem
I made a small shoot script but for some reason when i try to use a gui for ammo it doesn't update properly.I have 36/360 but when i shoot it does update but 36 remains there so we have 36 on top of 35. Whats going on? heres my code:
var FullAmmo : int = 360;
public var Clip : int = 36;
var ClipLeft : int = FullAmmoClip - Clip;
private var nextbulletTraceTime : float;
var ammo :GUIText;
function Update () {
ClipLeft = FullAmmoClip - Clip;
accuracy = Mathf.Clamp01(accuracy);
if (Input.GetKeyDown(KeyCode.R))
{
FullAmmo-=ClipLeft;
Clip = 36;
}
if (Clip<=0)
{
on = false;
}
if (on){
if(Time.time > nextbulletTraceTime){
rate = Mathf.Max(rate, 1.0);
nextbulletTraceTime = Time.time + (1.0 / rate);
var newBulletTrace : GameObject = Instantiate(bulletTracePrefab,transform.position,transform.rotation);
Clip = Clip - 1;
var bulletVelocity : Vector3 = newBulletTrace.GetComponent("bulletTrace").velocity;
var badAim : float = (1-accuracy);
badAim *= newBulletTrace.GetComponent("bulletTrace").bulletSpeed * 0.05;
bulletVelocity += newBulletTrace.transform.right * Random.Range(-badAim,badAim);
bulletVelocity += newBulletTrace.transform.up * Random.Range(-badAim,badAim);
newBulletTrace.GetComponent("bulletTrace").velocity = bulletVelocity;
}
}
}
function OnGUI()
{
//GUI.Box(Rect(Screen.width - 100,Screen.height - 50,100,50),("Clip");
//GUI.Box (Rect (10,10,100,90),"" + Clip.ToString() +"/"+FullAmmo.ToString()+"");
GUILayout.Label( "Ammo " + Clip.ToString()+"/"+ FullAmmo.ToString());
}
also do you mean that, the 360 part is the only part which decreases and not the 36. So what you want is the 36 to update? Just so i can get a good view on what you want :)
In addition could you please put up fullammoclip variable declaration. Then i can do it for you.
Answer by APenguin · Feb 07, 2014 at 11:26 PM
Okay you had quite a few variables which weren't really needed. So i took them out. Maybe next time when you do coding, which is great you are doing yourself, think it through what is happening. But here is the change which i made. I hope it is what you are after:
if (Input.GetKeyDown(KeyCode.R))
{
Clip -= 1;
//FullAmmo-=ClipLeft;
//clip = 36;
}
if (Clip<=0)
{
Clip = 36;
FullAmmo -= 1;
}
Basically what happens here is that on r down only the Clip gets minus by 1. When Clip <= 0 then reset the Clip back to 36, and minus FullAmmo by 1.
thanks, but my problem wasn't with tht code, i debugged it and it was working perfectly, it was the GUI wasn't showing the ammo correctly
it was updating but say I shot, it would $$anonymous$$us one and 35 would show but so would 36 so they would keep building up
I tried the code i answered with, and it worked perfectly. So when i pressed R, it would show 35/360, then all way to 1/360. Once i reached 0 it would go to 36/359. I didn't see any 36 with the 35 or any other number
Oh in fact, take out Clip = 36 when the key R is down. Because that keeps Clip at 36 when you shoot which shouldn't be right.
Your answer
Follow this Question
Related Questions
GuiTexture Width Change 1 Answer
Feast or Famine with GuiText 1 Answer
How to make a simple inventory?!? Beginner coder JS 1 Answer
GUI text Stays in place 1 Answer