Why doesnt the value change?

Im trying to break a block but the value in BlockData doesnt change.

(on all blocks)

public class BlockData : MonoBehaviour {

public float health;
public float strength;
}

This is called everytime i click a button, the Debug ges called but the health doesnt change. A block who has 1 health gets instant destroyed but a block with 5 health never dies… Why desnt this work?

void BreakBlock()
{

    RaycastHit hit;
    if (Physics.Raycast(cam.transform.position, cam.transform.forward, out hit, range, LayerMask.GetMask("Breakable")))
    {
        float health = hit.transform.gameObject.GetComponent<BlockData>().health;
        float strength = hit.transform.gameObject.GetComponent<BlockData>().strength;

        if (breakingStrength > strength)
        {
            Debug.Log("attack");
            health = health - blockDamage;

            if(health < 1)
            {
                Destroy(hit.transform.gameObject);
            }   
        }
    }
}

}

Because you never reassign the health to the gameobject hit. Your health variable is only set locally (so if health=1, gameobject is destroyed, else it is never updated).

Hope it helps :wink:

I made a small change and it works now. Thx for help!