Problem with raycast

Hi all, I did a script for destroy the game object, each game object has it’s resistence, every click remove 1 of resistence, the problem is: if there are 2 game objects called wood (each wood has 5 of resistence) if I click 3 times in the first wood, i need to click only 2 times in the second wood to destroy it. Sorry for my english, This is the code:

using UnityEngine;
using System.Collections;

public class DestroyBlock : MonoBehaviour
{
    int Wood_Resistence = 5;     //The wood resistence
	
	private Ray ray;
	private RaycastHit hit;
	
	void Update()
	{
        ray = Camera.main.ScreenPointToRay(Input.mousePosition);
        
		if(Physics.Raycast(ray,out hit, 5.0f) && Input.GetMouseButtonDown (0))
		{
		    //WOOD
			if(hit.collider.gameObject.name == "Wood")
			{
			    Wood_Resistence--;
				if(Wood_Resistence == 0)
				{
				    Destroy(hit.collider.gameObject);
					Wood_Resistence = 5;
				}
			}
		}		
    }
}

You have to check you actually hit the object to which the script is attached.

Instead of

if(hit.collider.gameObject.name == "Wood")

Use

if(hit.collider == this.collider)

You need to different script for decrement the Wood_Resistence.
first : create a empty game object and attach given scripts bellow

public class TapOnScreen : MonoBehaviour {

private Ray ray;
private RaycastHit hit;

void Update()
{
	ray = Camera.main.ScreenPointToRay(Input.mousePosition);
		 
	if(Physics.Raycast(ray,out hit, Mathf.Infinity) && Input.GetMouseButtonDown (0))
	{
		
		//WOOD
		if(hit.collider.gameObject.name == "Wood")
		{
			WoodHealth wh= hit.collider.gameObject.GetComponent<WoodHealth>();
			wh.OnHitDamage(1);
		}
	}
}

}

2nd: attach bellow script with all your wood object

public class WoodHealth : MonoBehaviour {

private int health=5;
public void OnHitDamage(int amount){
	health-=amount;
	if(health<=0){
		Destroy(gameObject);
		return;
	}
	//do all you needs to do
}

}

This is because your Wood_resistance property is part of your DestroyBlock class. So each block you click on will lower the Wood_resistance by one within your DestroyBlock and not on an individual wood block.

To resolve this, create a simple class, call it something like WoodResistance, give this class a property, something like Wood_resistance, like this:

public class WoodResistance : MonoBehaviour
{

    public int Wood_resistance = 5;

}

Then, in your RayCast, find the WoodReistance class as a component of the gameObject that you hit. Something like this:

if(Physics.Raycast(ray,out hit, 5.0f) && Input.GetMouseButtonDown (0))
    {
        //WOOD
        if(hit.collider.gameObject.name == "Wood")
        {
            //find the WoodResistance component of the wood
            WoodResistance wr = hit.collider.gameObject.GetComponent<WoodResistance>();

            //decrement the WOOD's own property
            wr.Wood_Resistence--;

            //if the WOOD's own property is zero, destroy it
            if(wr.Wood_Resistence == 0)
            {
                Destroy(hit.collider.gameObject);
            }
        }
    } 

Make sure to give your blocks of wood the WoodResistance class for this to work.