- Home /
Adding Ammo to make gun loose/add ammo as used/picked up
I have a gun that has many different features of shooting but I can not seem to find out how and where to fix so I can see how much ammo and bullets I have left. I need a simple GUIText and nothing else but from what I have tried I just could not figure out where and how. I hope any of you could help me. Thanks PS. This is not my personal script.
This is a part of the script.
void MachineGun_Fire(){
if (bulletsLeft <= 0)
{
StartCoroutine("reload");
return;
}
// If there is more than one bullet between the last and this frame
// Reset the nextFireTime
if (Time.time - fireRate > nextFireTime)
nextFireTime = Time.time - Time.deltaTime;
// Keep firing until we used up the fire time
while (nextFireTime < Time.time)
{
switch (typeOfBullet)
{
case BulletType.Physical:
StartCoroutine("FireOneShot"); // fire a physical bullet
break;
case BulletType.Raycast:
StartCoroutine("FireOneRay"); // fire a raycast.... change to FireOneRay
break;
default:
Debug.Log("error in bullet type");
break;
}
shotsFired++;
bulletsLeft--;
nextFireTime += fireRate;
EjectShell();
Kick();
}
}
Answer by l5p4ngl312 · Jul 14, 2013 at 11:27 PM
Just make a new guiText (a gameObject with a guiText component). Make a variable in your script: public GameObject guiTextObject and put your guiText object in that slot in the editor. Then in your script just use guiTextObject.guiText.text = bulletsLeft.ToString();
public GameObject guiTextObject;
void MachineGun_Fire(){
if (bulletsLeft <= 0)
{
StartCoroutine("reload");
return;
}
// If there is more than one bullet between the last and this frame
// Reset the nextFireTime
if (Time.time - fireRate > nextFireTime)
nextFireTime = Time.time - Time.deltaTime;
// Keep firing until we used up the fire time
while (nextFireTime < Time.time)
{
switch (typeOfBullet)
{
case BulletType.Physical:
StartCoroutine("FireOneShot"); // fire a physical bullet
break;
case BulletType.Raycast:
StartCoroutine("FireOneRay"); // fire a raycast.... change to FireOneRay
break;
default:
Debug.Log("error in bullet type");
break;
}
shotsFired++;
bulletsLeft--;
guiTexObject.guiText.text = bulletsLeft.ToString();
nextFireTime += fireRate;
EjectShell();
Kick();
}
}
Now all you have to do is make a new guiText (GameObject -> Create Other -> GuiText), position it how you want, and then select the object that contains your gun scripIn the editor drag the new guiText object to where it says guiTextObject in the inspector. If you don't know how to do that you probably need to watch some more basic Unity tutorials. Here's more info on guiTexts: http://docs.unity3d.com/Documentation/Components/class-GuiText.html
Could you give a small example maybe? I'm still learning in school and the whole system is not very good in it's $$anonymous$$ching. Appreciate your quick answer.
Your answer
Follow this Question
Related Questions
How to sprint while holding a weapon? 0 Answers
Delayed Rotation 0 Answers
How to make a gun work with camera? 1 Answer
Gun is not rotating while player is in a animation 0 Answers
Simulate 'Child' Effect by Script 2 Answers