I have a GameObject “gun” with a script on it instantiate a projectile with a ‘health’, however it appears to be treating health as a global variable and causing the gun to shoot bullets that all share the same health pool. The initial health is stored on the gun itself, so that all bullets shot from the gun have this as their initial health. Instead, the health on the gun goes down as well.
The health is supposed to act as a piercing effect, allowing a bullet to go through a certain number of colliders before destroying itself.
Gun Script
[System.Serializable]
public class Weapon
{
public int damage;
public int health;
//etc, more public variables here
}
void public class WepSys
{
public Weapon wep;
void Shoot ()
{
GameObject shot = (GameObject) Instantiate(object, position, rotation);
shot.GetComponent<ProjectileMover> ().wep = wep;
shot.GetComponent<ProjectileMover> ().iniVel = GetComponent<Rigidbody>().velocity;
}
}
ProjectileMover Script
public class ProjectileMover : MonoBehaviour
{
public Weapon wep;
void OnTriggerEnter( Collider other )
{
if (wep.health > 0)
{
wep.health--;
}
else
{
Destroy (this);
}
}
}