Hey can someone please help me with my ammobar
im trying to create an ammobar to keep track of my ammo and i set the value of the bar to my guns ammo but the problem is is that my gun is instantiated after the game starts and if i put the gun prefab as the gun it wont work please help if you know a solution.
public class AmmoBar : MonoBehaviour { public Slider ammoBar; Gun gunAmmo; public Gun gunn;
private void Start()
{
gunAmmo = GetComponent<Gun>();
}
public void Update()
{
ammoBar.value = gunn.ammo;
} }
public class GunController : MonoBehaviour { public Transform weaponHold; public Gun startingGun; Gun equippedGun; AmmoBar ammoBar; void Start() { if (startingGun != null) { EquipGun(startingGun); } ammoBar = GetComponent(); equippedGun = ammoBar. }
public void EquipGun(Gun gunToEquip)
{
if (equippedGun != null)
{
Destroy(equippedGun.gameObject);
}
equippedGun = Instantiate(gunToEquip, weaponHold.position, weaponHold.rotation) as Gun;
equippedGun.transform.parent = weaponHold;
}
public void Shoot()
{
if (equippedGun != null)
{
equippedGun.Shoot();
}
}
}
public class Gun : MonoBehaviour {
public int ammo;
public Transform muzzle;
public Projectile projectile;
public float msBetweenShots = 100;
public float muzzleVelocity = 35;
GunController gun;
float nextShotTime;
public void Start()
{
ammo = 20;
}
public void Shoot()
{
{
if (Time.time > nextShotTime)
{
if (ammo >= 1)
{
ammo--;
nextShotTime = Time.time + msBetweenShots / 1000;
Projectile newProjectile = Instantiate(projectile, muzzle.position, muzzle.rotation) as Projectile;
newProjectile.SetSpeed(muzzleVelocity);
}
}
}
}
}
,public class AmmoBar : MonoBehaviour { public Slider ammoBar; Gun gunAmmo; public Gun gunn;
private void Start()
{
gunAmmo = GetComponent<Gun>();
}
public void Update()
{
ammoBar.value = gunn.ammo;
} }
public class Gun : MonoBehaviour {
public int ammo;
public Transform muzzle;
public Projectile projectile;
public float msBetweenShots = 100;
public float muzzleVelocity = 35;
GunController gun;
float nextShotTime;
public void Start()
{
ammo = 20;
}
public void Shoot()
{
{
if (Time.time > nextShotTime)
{
if (ammo >= 1)
{
ammo--;
nextShotTime = Time.time + msBetweenShots / 1000;
Projectile newProjectile = Instantiate(projectile, muzzle.position, muzzle.rotation) as Projectile;
newProjectile.SetSpeed(muzzleVelocity);
}
}
}
}
}
public class GunController : MonoBehaviour { public Transform weaponHold; public Gun startingGun; Gun equippedGun;
void Start()
{
if (startingGun != null)
{
EquipGun(startingGun);
}
}
public void EquipGun(Gun gunToEquip)
{
if (equippedGun != null)
{
Destroy(equippedGun.gameObject);
}
equippedGun = Instantiate(gunToEquip, weaponHold.position, weaponHold.rotation) as Gun;
equippedGun.transform.parent = weaponHold;
}
public void Shoot()
{
if (equippedGun != null)
{
equippedGun.Shoot();
}
}
}
here is the actual guncontroller script public class GunController : $$anonymous$$onoBehaviour {
public Transform weaponHold;
public Gun startingGun;
Gun equippedGun;
void Start()
{
if (startingGun != null)
{
EquipGun(startingGun);
}
}
public void EquipGun(Gun gunToEquip)
{
if (equippedGun != null)
{
Destroy(equippedGun.gameObject);
}
equippedGun = Instantiate(gunToEquip, weaponHold.position, weaponHold.rotation) as Gun;
equippedGun.transform.parent = weaponHold;
}
public void Shoot()
{
if (equippedGun != null)
{
equippedGun.Shoot();
}
}
}
Answer by boyblakk · Apr 26, 2019 at 01:55 PM
sorry ignore the two lines in the GunController Script about the ammobar i was experimenting and forgot to delete
Your answer
![](https://koobas.hobune.stream/wayback/20220612200242im_/https://answers.unity.com/themes/thub/images/avi.jpg)