- Home /
FPSPlayer script not seeing weapon
I am using unity 3.3 and I am using the FPS tutorial. The tutorial works, but I decided to make the rocket launcher display the ammo in numbers just like the machine gun does. The problem is that when I switch weapons, it always shows the ammo for the rocket launcher.
Here is where the problem is. I think the two if statements below aren't working right because the rocket launcher part is always executed. Can someone show me how to fix this? Thanks.
function UpdateGUI () {
// Update health gui
// The health gui is rendered using a overlay texture which is scaled down based on health
// - Calculate fraction of how much health we have left (0...1)
var healthFraction = Mathf.Clamp01(hitPoints / maximumHitPoints);
// - Adjust maximum pixel inset based on it
healthGUI.pixelInset.xMax = healthGUI.pixelInset.xMin + healthGUIWidth * healthFraction;
// Update machine gun gui
// Machine gun gui is simply drawn with a bullet counter text
if (machineGun) {
bulletGUI.text = machineGun.bulletsLeft.ToString();
clipGUI.text = machineGun.clips.ToString();
}
// Update rocket gui
// This is changed from the tutorial PDF. You need to assign the 20 Rocket textures found in the GUI/Rockets folder
// to the RocketTextures property.
if (rocketLauncher) {
//rocketGUI.UpdateRockets(rocketLauncher.ammoCount);
bulletGUI.text = rocketLauncher.ammoCount.ToString();
}
}
Try to switch the order of the if's just to see if the machineGun is shown ins$$anonymous$$d. Usually it's better to have one only variable to control which weapon is selected, since only one will be available at a time. You can have an int variable - say, weaponType - and decide what to do with a switch statement:
switch (weaponType){
case 1:
// display machineGun ammo
break;
case 2:
// display rockets
break;
}
Answer by Syanps3 · Jun 21, 2011 at 12:14 AM
I had to be able to access the var from another script to make this work. I was using GetComponent and apparently I should have been using GetComponentInChildren. Now it works. Here it is.
var wep = GetComponentInChildren(PlayerWeapons); //This part drove me insane to find out
switch (wep.weaponType){
case 0:
// display machineGun ammo
bulletGUI.text = machineGun.bulletsLeft.ToString();
clipGUI.text = machineGun.clips.ToString();
break;
case 1:
// display rockets
bulletGUI.text = rocketLauncher.ammoCount.ToString();
break;
}