Hi!
I have here these two scripts:
Script A - This goes on the Tree object.
public class CutTree : MonoBehaviour
{
public AudioClip ChopSound;
public GameObject Tree;
public static int amountHitTree;
void Start()
{
Tree = this.gameObject;
amountHitTree = 0;
}
void OnMouseOver()
{
if(Input.GetMouseButtonDown(0))
{
_Manager.Wood += 1;
audio.PlayOneShot(ChopSound);
amountHitTree += 1;
}
}
void Update()
{
if (amountHitTree == 5)
{
Destroy(gameObject);
}
}
}
and Script B - This goes on an empty gameObject.
public class _Manager : MonoBehaviour
{
public static int Wood;
public static int Stone;
public static int Meat;
public AudioClip treeFallSound;
private bool hasPlayed = false;
void OnGUI()
{
GUILayout.Label("Wood: " + _Manager.Wood);
GUILayout.Label("Stone: " + _Manager.Stone);
}
void Update()
{
if(CutTree.amountHitTree == 5)
{
if(!hasPlayed)
{
audio.PlayOneShot(treeFallSound);
hasPlayed = true;
}
}
}
}
I have the ‘amountHitTree’ int set to static because of sound issues, and a OneShot playing an infinite amount of times every frame.
The problem is that when I hit one tree 5 times, it destroys all the others. I know why; this is because the ‘amountHitTree’ int is static–is there any other way i can fix this?