Why my pick deletes it own?

heres my code :

var cubeStrength : float;
var Pickaxe : GameObject;
 
function Update() 
{
 if(Input.GetMouseButtonDown(1)) Erase();
 
} 
function Erase()
{
      cubeStrength --;
    if (cubeStrength == 0)
        Destroy (gameObject);
}

Im trying to make the pick deletes the cube floor but the pickaxe deletes it self,is there any ways to fix this from deleting my pickaxe?

var PickAxe : GameObject; 
var range : float = Mathf.Infinity; 
var hit : RaycastHit; 
var BlockStrength : float;

function Update () 
{ 
if(Input.GetButton("Fire1")){ 
} 

if(Input.GetMouseButtonDown(0)){ 
Erase(); 
} 
} 


function Erase() 
{ 
 BlockStrength --;
if (BlockStrength == 0)
if(HitBlock()) 
Destroy(hit.transform.gameObject); 
} 


function HitBlock() : boolean 
{ 
return Physics.Raycast(transform.position, transform.forward, hit, range); 
}

I just fixed my code but it just deletes single block.How can i make it deletes more then one block?

Consider clicking the checkmark next to the answer unless you are absolutely not satisfied with it, but I don't think you will get any more detailed anwers since there is not much to work with here.

1 Answer

1

My guess is that your script is attached to your pickaxe, and thus is Destroy (gameObject); resulting in the deletion of your pickaxe.

Attaching the script to to your cube will go more into the direction you are heading to, but a click on the mouse will result in lowering the cubeStrength of every cube the script is attached to. So you need some serious rethinking of your code flow.

take a look at this: - [GameObject.find][1] - [Pixaxe Trigger enter][2] [1]: http://docs.unity3d.com/Documentation/ScriptReference/GameObject.FindGameObjectsWithTag.html [2]: http://docs.unity3d.com/Documentation/ScriptReference/Collider.OnTriggerEnter.html

http://unity3d.com/learn/tutorials/modules/beginner/scripting/destroy , look at this video and DestroyOther. To find the other object (the cube in this case) you will need to use a collider in combination with onTriggerEnter or perhaps Raycasts. It's to much to explain this here, so I suggest following the tutorials. Scripting reference about onTriggerEnter() : http://docs.unity3d.com/Documentation//ScriptReference/Collider.OnTriggerEnter.html