Image Bullets in quantity

Hi, forgive the spelling but I don’t speak English. I need a little help: I ​​have a firearm script and among the many changes I would like to add to the count of ammunition I have already done, the relative image created in Canvas. Since the ammunition is infinite as mentioned in the script, and I decide how much ammunition a weapon can have, and it is already made into a public variant, I would like to do the same thing with the image of the bullet, to be used for a bag containing 4 weapons ( or more if you want). Thank you very much for your attention. Given the script in question, I hope you can help me, goodbye.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class GunArm : MonoBehaviour {

public float damage = 10f;
public float range = 100f;
public float fireRate = 15f;
public float impactForce = 30f;

public GameObject Bullet_Emitter;
public GameObject Bullet;
public float Bullet_Forward_Force;

public GameObject BulletHole;

public Text AmmoText; //Se si vuole costruire una Text per descrivere l'arma

//public string BoxDescription;

[SerializeField]
private string _fireAnimTriggerName;

public int maxAmmo = 10;
private int currentAmmo;
public float reloadTime = 1f;

public AudioClip ReloadGun;
private bool isReloading = false;

public Camera fpsCam;
public ParticleSystem muzzleFlash;
public GameObject impactEffect;

private float nextTimeToFire = 0f;

public Animator animator;

void Start ()
{
	currentAmmo = maxAmmo;
	AmmoText.text ="Ammo = " + currentAmmo.ToString() + " / " +maxAmmo;// ∞ Simbolo per indicare le munizioni infinite
}

void OnEnable()
{
	isReloading = false;
	animator.SetBool ("Reloading", false);
}

//void OnGUI()
//{
	//GUI.Label (new Rect (28, 480, 200, 80), BoxDescription);//10, 10, 150, 50
	//GUI.Label(new Rect(28,545,200,80), "Ammo =  "+currentAmmo+" / Max "+maxAmmo);//+maxAmmo(se si vuol aggiungere all'interno della parentesi)
//}

// Update is called once per frame
void Update () {

	if (isReloading)
		return;

	if (currentAmmo <= 0)
	{
		StartCoroutine (Reload ());
		return;
	}

	if (Input.GetButton ("Fire1") && Time.time >= nextTimeToFire)
	{
			if (animator != null)
				animator.SetTrigger ("fire");
		nextTimeToFire = Time.time + 1f / fireRate;
		Shoot ();
		GetComponent<AudioSource> ().Play ();

		}

}

IEnumerator Reload(){
	
	isReloading = true;

	GetComponent<AudioSource> ().PlayOneShot (ReloadGun);
	Debug.Log ("Reloading...");

	animator.SetBool ("Reloading", true);
	yield return new WaitForSeconds (reloadTime - .25f);

	animator.SetBool ("Reloading", false);
	yield return new WaitForSeconds (.25f);

	currentAmmo = maxAmmo;
	AmmoText.text ="Ammo = " + currentAmmo.ToString() + " / "+maxAmmo;
	isReloading = false;

}

void Shoot()
{
	GameObject Temporary_bullet_Handler;//
	Temporary_bullet_Handler = Instantiate (Bullet,Bullet_Emitter.transform.position,Bullet_Emitter.transform.rotation)as GameObject;
	//Temporary_bullet_Handler.transform.Rotate(Vector3.left * 90);//
	Rigidbody Temporary_Rigidbody;//
	Temporary_Rigidbody = Temporary_bullet_Handler.GetComponent<Rigidbody>();
	Temporary_Rigidbody.velocity = Bullet_Emitter.transform.forward * Bullet_Forward_Force;
	Destroy(Temporary_bullet_Handler, 4.0f);//

	muzzleFlash.Play ();
	currentAmmo --;
	AmmoText.text ="Ammo = " + currentAmmo.ToString() + " / "+maxAmmo;
	RaycastHit hit;
	if (Physics.Raycast (fpsCam.transform.position, fpsCam.transform.forward, out hit, range))
		Instantiate (BulletHole, hit.point, Quaternion.FromToRotation (Vector3.up, hit.normal));
	{
		Debug.Log (hit.transform.name);
	
		Target target = hit.transform.GetComponent<Target> ();
		if (target != null) 
		{
			target.TakeDamage (damage);
		}

		if (hit.rigidbody != null) 
		{
			hit.rigidbody.AddForce (-hit.normal * impactForce);
		}

		GameObject impactGO = Instantiate (impactEffect, hit.point, Quaternion.LookRotation (hit.normal));
		Destroy (impactGO, 2f);
	}
}

}

Create a list to hold your bullets.

Stack<GameObject> bullets;

In your Reload() method:

bullets.Clean();
for (int i = 0; i < maxAmmo; i++){
     GameObject Temporary_bullet_Handler;//
     Temporary_bullet_Handler = Instantiate (Bullet,Bullet_Emitter.transform.position,Bullet_Emitter.transform.rotation)as GameObject;
     //Temporary_bullet_Handler.transform.Rotate(Vector3.left * 90);//
     Rigidbody Temporary_Rigidbody;//
     //this line here is not necessary you could add Rigidbody component here instead
     //Temporary_Rigidbody = Temporary_bullet_Handler.GetComponent<Rigidbody>();
     bullets.Push(Temporary_bullet_Handler);
}

Now you have a collection of bullets. In your shoot method you could do this:

if(bullets.Count > 0){
    var bullet = bullets.Pop(); //get the first bullet and remove from stack
    bullet.GetComponent<Rigidbody>().velocity = Bullet_Emitter.transform.forward * Bullet_Forward_Force;
    Destroy(bullet, 4.0f);//
} else {
   Reload(); //or whereever you want to do this
}

PS: Ofc. you don’t necessarily instantiate and destroy bullets everytime. You could store them and use them over and over with activating/deactivating them.

I did as you told me, I return the script to see if I did it right. I’m sorry but it doesn’t work, errors appear and the weapon doesn’t charge anymore, keep firing.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class GunArm1 : MonoBehaviour {

public float damage = 10f;
public float range = 100f;
public float fireRate = 15f;
public float impactForce = 30f;

public GameObject Bullet_Emitter;
public GameObject Bullet;
public float Bullet_Forward_Force;

public GameObject BulletHole;

public Text AmmoText; //Se si vuole costruire una Text per descrivere l'arma
//public Texture2D AmmoGUI;//Optional

[SerializeField]
private string _fireAnimTriggerName;

public int maxAmmo = 10;
private int currentAmmo;//currentAmmo
public float reloadTime = 1f;

public AudioClip ReloadGun;
private bool isReloading = false;

public Camera fpsCam;
public ParticleSystem muzzleFlash;
public GameObject impactEffect;

private float nextTimeToFire = 0f;

Stack<GameObject> bullets;

public Animator animator;

void Start ()
{
	currentAmmo = maxAmmo;
	AmmoText.text ="" + currentAmmo.ToString() + " /";// ∞ Simbolo per indicare le munizioni infinite
}

void OnEnable()
{
	isReloading = false;
	animator.SetBool ("Reloading", false);
}

//optional
//void OnGUI()
//{
	//GUI.Label(new Rect(15, Screen.height - 90,60,30),currentAmmo.ToString() + "/" + maxAmmo.ToString());

	//int index = 1;
	//while(index <= currentAmmo);
	//index++;
	//GUI.DrawTexture(new Rect ( (index * 9), Screen.height - 70,40,60), AmmoGUI);
//}

// Update is called once per frame
void Update () {

	if (isReloading)
		return;

	if (currentAmmo <= 0)
	{
		StartCoroutine (Reload ());
		return;
	}

	if (Input.GetButton ("Fire1") && Time.time >= nextTimeToFire)
	{
			if (animator != null)
				animator.SetTrigger ("fire");
		nextTimeToFire = Time.time + 1f / fireRate;
		Shoot ();
		GetComponent<AudioSource> ().Play ();
	}

}

IEnumerator Reload(){
	
	isReloading = true;

	GetComponent<AudioSource> ().PlayOneShot (ReloadGun);
	Debug.Log ("Reloading...");

	animator.SetBool ("Reloading", true);
	yield return new WaitForSeconds (reloadTime - .25f);

	animator.SetBool ("Reloading", false);
	yield return new WaitForSeconds (.25f);

	currentAmmo = maxAmmo;

	AmmoText.text ="" + currentAmmo.ToString() + " /";

	for (int i = 0; i < maxAmmo; i++){
		GameObject Temporary_bullet_Handler;//
		Temporary_bullet_Handler = Instantiate (Bullet,Bullet_Emitter.transform.position,Bullet_Emitter.transform.rotation)as GameObject;
		//Temporary_bullet_Handler.transform.Rotate(Vector3.left * 90);//
		Rigidbody Temporary_Rigidbody;//
		//this line here is not necessary you could add Rigidbody component here instead
		//Temporary_Rigidbody = Temporary_bullet_Handler.GetComponent<Rigidbody>();
		bullets.Push(Temporary_bullet_Handler);
	}
	//AmmoText.text ="Ammo = " + currentAmmo.ToString() + " / "+maxAmmo; //Stringa precedente
	isReloading = false;
}

void Shoot()
{
	GameObject Temporary_bullet_Handler;//
	Temporary_bullet_Handler = Instantiate (Bullet,Bullet_Emitter.transform.position,Bullet_Emitter.transform.rotation)as GameObject;
	//Temporary_bullet_Handler.transform.Rotate(Vector3.left * 90);//
	Rigidbody Temporary_Rigidbody;//
	Temporary_Rigidbody = Temporary_bullet_Handler.GetComponent<Rigidbody>();
	Temporary_Rigidbody.velocity = Bullet_Emitter.transform.forward * Bullet_Forward_Force;
	Destroy(Temporary_bullet_Handler, 4.0f);

	if(bullets.Count > 0){
		var bullet = bullets.Pop(); //get the first bullet and remove from stack
		bullet.GetComponent<Rigidbody>().velocity = Bullet_Emitter.transform.forward * Bullet_Forward_Force;
		Destroy(bullet, 4.0f);//
	} else {
		Reload(); //or whereever you want to do this
	}

	muzzleFlash.Play ();
	currentAmmo --;
	AmmoText.text ="" + currentAmmo.ToString() + " /";
	RaycastHit hit;
	if (Physics.Raycast (fpsCam.transform.position, fpsCam.transform.forward, out hit, range))
		Instantiate (BulletHole, hit.point, Quaternion.FromToRotation (Vector3.up, hit.normal));
	{
		Debug.Log (hit.transform.name);
	
		Target target = hit.transform.GetComponent<Target> ();
		if (target != null) 
		{
			target.TakeDamage (damage);
		}

		if (hit.rigidbody != null) 
		{
			hit.rigidbody.AddForce (-hit.normal * impactForce);
		}

		GameObject impactGO = Instantiate (impactEffect, hit.point, Quaternion.LookRotation (hit.normal));
		Destroy (impactGO, 2f);
	}
}

}