- Home /
ammo count help
I am currently using the first person shooter tutorial, and though i know its pretty out of date thats irrelevant to my problem. On the GUI it shows the current ammo count of my first weapon. Ive added in a heavy rifle right now, and the ammo for that is showing. However im trying to get it to change to my regular rifle when that is equipped. My current code isnt changing the two. The code I have is as follows:
private var machineGun : MachineGun; private var rocketLauncher : RocketLauncher; private var heavyMachineGun : HeavyMachineGun;
function Awake () { machineGun = GetComponentInChildren(MachineGun); rocketLauncher = GetComponentInChildren(RocketLauncher); heavyMachineGun = GetComponentInChildren(HeavyMachineGun); }
if (machineGun) { bulletGUI.text = machineGun.GetRegularBulletsLeft().ToString(); } if (heavyMachineGun) { bulletGUI.text = heavyMachineGun.GetHeavyBulletsLeft().ToString(); }
When you post codes, highlight the code sectio and press ctrl+k or the 101010 button. that makes it easier to read and you are more likely to get help.
Answer by Piflik · Apr 15, 2012 at 04:34 PM
Does your script compile? The reason I ask is that MachineGun, RocketLauncher and HeavyMachineGun are not valid variable types.
You would have to declare the variables like this:
private var machineGun : Transform = null;
private var rocketLauncher : Transform = null;
private var heavyMachineGun : Transform = null;
function Awake() {
machineGun = GetComponentInChildren(MachineGun);
rocketLauncher = GetComponentInChildren(RocketLauncher);
heavyMachineGun = GetComponentInChildren(HeavyMachineGun);
}
function Update() {
if(machineGun != null)
bulletGUI.text = machineGun.GetRegularBulletsLeft().ToString();
if(heavyMachineGun != null)
bulletGUI.text = heavyMachineGun.GetHeavyBulletsLeft().ToString();
}
the script did compile before, I tried the variable declaration like above and I got the error: "Cannot convert '$$anonymous$$achineGun' to 'UnityEngine.Transform'" etc for the rest.