- Home /
Duplicate Question
Destroy (gameObject) Help
I am trying to make practice orbs to shoot at, when armor reaches 0 they should be destroyed. Problem is, once you kill one orb, they all die. Here is my rough armor script that is attached to each practice orb:
public GameObject Target;
public int armor;
public static int armorRating;
public static int currArmor;
void Start ()
{
armorRating = armor;
currArmor = armorRating;
}
void Update ()
{
if(currArmor <= 0 )
{
Destroy(gameObject);
Debug.Log("Practice Item Destoyed");
}
}
public static void TakeDamage (int amount)
{
currArmor -= amount;
Debug.Log (currArmor);
}
I cannot seem to figure out where I am going wrong, any help would be greatly appreciated. Thank you
I am closing that question as it falls into the already full bucket of GetComponent/Static/Script interaction questions.
Have a little faith in you, find them and read them.
Answer by Andres-Fernandez · Jan 23, 2015 at 07:38 AM
Just remove the static modifier from your currArmor variable (and google it, since it seems you are not using it properly)
currArmor has to be static because I call it in my TakeDamage function which is static, and TakeDamage has to be static so that I can enter the damage amount from the bullet. or am I doing that wrong as well?
The function doesn't need to be static (if it's public, then it can be called from any other script). And the variable can't be static, since a static member will be shared by all instances of the class, therefore all your orbs will share the same currArmor value (you decrease the value in one orb, but all orbs share the same value...). Check here.
Thanks for your help I was understanding statics wrong i will have to change a few other scripts now but it worked great!