Reference variable on script from another script JAVASCRIPT

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

Use the generic version of getComponent Unity - Scripting API: GameObject.GetComponent

(Shows you the example in the second part, switch to JS if it’s not already showing)

Also, you didn’t mention, but is your debug even printing?

And…just as a friendly note, UnityScript is dead, not even an option in the newer versions of Unity to create scripts with it. You may want to switch to c# if you can (you’ll also find more help with it).

I would also try to do something with that GameObject.Find as it can be slow.

Yes the debug call is working.

Do I need all the code from the generic version, or just the
var hinge: HingeJoint = gameObject.GetComponent(“HingeJoint”) as HingeJoint;
if (hinge != null)
hinge.useSpring = false;
}

Part, I cant seem to get it into my code to work

The generic version uses the <> brackets. It’s the second example.

Use the first and check if treeController is null to make sure it’s getting the component.

var treeController: TreeController= tree.GetComponent.<TreeController>();

Similar to what you are doing.

tree.GetComponent.<TreeController>().treeHealth -= 1;

Since the debug is working, are you getting any errors?

not getting any errors
with the code you gave, how would i reduce the health?

Assuming treeHealth is a public variable,

treeController.treeHealth -= 1; is fine. However, I generally suggest you have a method on the script that you pass “damage” to so it can handle what happens when that health reaches 0 on the object.

Since you aren’t getting any errors. What are you looking at that shows it’s not reducing health?

the set up was for the tree to take damage when hit and when the health gets to 0 it would fall
the tree health is shown in the inspector, and when set to 0 it falls
the raycast just doesn’t seem to damage the trees health