I just need to know why this script doesn’t work
var projectile : Rigidbody;
var ammo = 100;
var canShoot : boolean;
var subtractedAmmo : int;
var speed = 20;
function Start () {
canShoot = true;
}
function Update()
{
if(canShoot){
if( Input.GetButtonDown( "Fire1" ) ) {
var instantiatedProjectile : Rigidbody = Instantiate(
projectile, transform.position, transform.rotation );
instantiatedProjectile.velocity =
transform.TransformDirection( Vector3( 0, 0, speed ) );
Physics.IgnoreCollision( instantiatedProjectile. collider,
transform.root.collider );
--ammo;
}
}
if (ammo <= 0)
canShoot = false;
if (ammo > 100)
ammo = 100;
subtractedAmmo == (ammo -= 100); //this is the line of code that's not working
}
function AddAmmo(){
ammo += subtractedAmmo;
}
fafase
2
You don’t seem to use Addammo anywhere or I don’t see it.
subtractedAmmo == (ammo -= 100); is not working because you are comparing.
subtractedAmmo = (ammo -= 100);
But I try to understand what is going on at the end but am confused.
I would avoid cryptic code like the plague. It is a false economy to try and get every last ounce of performance out of code before it is functioning. In addition, modern compilers optimize the code for you. In my world, I always place curly braces after an “if” statement and would always replace:
ammo -=100
with
ammo = ammo - 100
also code like
y = (x -= 10)
I would replace with
y = x; x = x - 10;