I have a script that is meant to take health away from a rock that I can mine. It can only be mined when the player is 2 units away from the rock and only if the player is holding the mouse button down.
The problem that I am having is that the script takes away the health of every object with that script on instead of the object the player is mining.
Here’s the script:
public var stackPrefab : GameObject;
public var Health : float = 500;
private var CanCut : boolean = false;
function Start()
{
Health = 500;
}
function Update(){
var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
var hit : RaycastHit;
if (Physics.Raycast (ray, hit, 2)) {
CanCut = true;
}
else{
CanCut = false;
}
if(Input.GetMouseButton(0) && CanCut)
{
Health -=1;
}
if(Health < 0)
{
Health = 0;
}
if(Health == 430){
Instantiate(stackPrefab, transform.position + Vector3(1,0,4), transform.rotation);
}
if(Health == 320){
Instantiate(stackPrefab, transform.position + Vector3(3,0,4), transform.rotation);
}
if(Health == 240){
Instantiate(stackPrefab, transform.position + Vector3(2,0,4), transform.rotation);
}
if(Health == 130){
Instantiate(stackPrefab, transform.position + Vector3(0,0,4), transform.rotation);
}
if(Health == 0){
Instantiate(stackPrefab, transform.position + Vector3(4,0,4), transform.rotation);
Destroy(gameObject);
}
}