raycasthit questions

hi, i am trying to make some choppable trees my raycast do land on the tree and do detect it but i cant seem to remove health from it. My tree has a rigid body and a box colider. Any help would be appreciated , if you need more information or post isn’t complete enought please let me know.

#pragma strict

var rayLength = 10;


//private var treeScript : TreeController;

var tree : GameObject;

private var playerAnim : PlayerControl;

function Update()
{
	var hit : RaycastHit;
	var fwd = transform.TransformDirection(Vector3.forward);
	
	if(Physics.Raycast(transform.position, fwd, hit, rayLength))
	{
		if(hit.collider.gameObject.tag == "Tree")
		{
			//treeScript = GameObject.Find(hit.collider.gameObject.name).GetComponent(TreeController);
			tree = (hit.collider.gameObject);
			playerAnim = GameObject.Find("@Idle").GetComponent(PlayerControl);
			
			if(Input.GetButtonDown("Fire1") && playerAnim.canSwing == true)
			{
				//treeScript.treeHealth -= 1;
				tree.GetComponent(TreeController).treeHealth -= 1;
			}
		}
	}
}

//this script is on the tree and is called TreeController

var treeHealth : int = 5;

var logs : Transform;
var branch : Transform;
var tree : GameObject;

var speed : int = 8;

function Start()
{
	tree = this.gameObject;
	rigidbody.isKinematic = true;
}

function Update()
{
	if(treeHealth <= 0)
	{
		rigidbody.isKinematic = false;
		rigidbody.AddForce(transform.forward * speed);
		DestroyTree();
	}
}

function DestroyTree()
{
	yield WaitForSeconds(7);
	Destroy(tree);
	
	var position : Vector3 = Vector3(Random.Range(-1.0, 1.0), 0, Random.Range(-1.0, 1.0));
	Instantiate(logs, tree.transform.position + Vector3(0,0,0) + position, Quaternion.identity);
	Instantiate(logs, tree.transform.position + Vector3(2,2,0) + position, Quaternion.identity);
	Instantiate(logs, tree.transform.position + Vector3(5,5,0) + position, Quaternion.identity);
	
	Instantiate(branch, tree.transform.position + Vector3(0,0,0) + position, Quaternion.identity);
	Instantiate(branch, tree.transform.position + Vector3(2,2,0) + position, Quaternion.identity);
	Instantiate(branch, tree.transform.position + Vector3(5,5,0) + position, Quaternion.identity);
	
}

It works for me.

It looks like working code. I think what’s happening is that nothing happens to the tree when it loses health. So even though you are technically removing health, nothing happens as a result. Throw this code snippet into your TreeController code:

function Update() {
    if (treeHealth <= 0) {
        Destroy(gameObject);
    }
}

That basically means that if the tree were to ever be reduced to 0 health, it will destroy itself.