How do i reference a variable from another script or a script from another script.
This is the script I have with the tree health
#pragma strict
var treeHealth : int = 1;
var logs : Transform;
//var coconut : Transform;
var tree : GameObject;
var speed : int = 8;
function Start()
{
tree = this.gameObject;
GetComponent.<Rigidbody>().isKinematic = true;
}
function Update()
{
if(treeHealth <= 0)
{
GetComponent.<Rigidbody>().isKinematic = false;
GetComponent.<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(coconut, tree.transform.position + Vector3(0,0,0) + position, Quaternion.identity);
//Instantiate(coconut, tree.transform.position + Vector3(2,2,0) + position, Quaternion.identity);
//Instantiate(coconut, tree.transform.position + Vector3(5,5,0) + position, Quaternion.identity);
}
And I want to reduce the health of the tree from another script, this one
#pragma strict
var rayLength : int = 10;
private var playerAnim : PlayerControl;
var tree : GameObject;
var tree : TreeController;
function Update ()
{
var hit : RaycastHit;
var fwd = transform.TransformDirection(Vector3.forward)*10;
Debug.DrawRay (transform.position, fwd, Color.green);
//Debug.Log("Hit!");
if(Physics.Raycast(transform.position, fwd, hit, rayLength))
{
//Debug.Log("Hit!");
if(hit.collider.gameObject.tag == "Tree")
{
//Debug.Log("Hit!");
tree = (hit.collider.gameObject);
playerAnim = GameObject.Find("FPSArms_Axe@Idle").GetComponent(PlayerControl);
if(Input.GetKeyDown(KeyCode.Return) && playerAnim.canSwing == true)
{
tree.GetComponent(TreeController).treeHealth -= 1;
Debug.Log("health");
}
}
}
}
I have tried referencing the health with this code
tree.GetComponent(TreeController).treeHealth -= 1;
but it does not seem to reduce the health, is there another way to reference the script/health.
Thanks