Search Unity

Reference variable on script from another script JAVASCRIPT

Discussion in 'Scripting' started by Orthos30, Nov 28, 2017.

  1. Orthos30

    Orthos30

    Joined:
    Oct 24, 2017
    Posts:
    4
    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

    Code (JavaScript):
    1. #pragma strict
    2.  
    3. var treeHealth : int = 1;
    4.  
    5. var logs : Transform;
    6. //var coconut : Transform;
    7. var tree : GameObject;
    8.  
    9. var speed : int = 8;
    10.  
    11. function Start()
    12. {
    13.     tree = this.gameObject;
    14.     GetComponent.<Rigidbody>().isKinematic = true;
    15. }
    16.  
    17. function Update()
    18. {
    19.     if(treeHealth <= 0)
    20.     {
    21.         GetComponent.<Rigidbody>().isKinematic = false;
    22.         GetComponent.<Rigidbody>().AddForce(transform.forward * speed);
    23.         DestroyTree();
    24.     }
    25. }
    26.  
    27. function DestroyTree()
    28. {
    29.     yield WaitForSeconds(7);
    30.     Destroy(tree);
    31.    
    32.     var position : Vector3 = Vector3(Random.Range(-1.0, 1.0), 0, Random.Range(-1.0, 1.0));
    33.     Instantiate(logs, tree.transform.position + Vector3(0,0,0) + position, Quaternion.identity);
    34.     Instantiate(logs, tree.transform.position + Vector3(2,2,0) + position, Quaternion.identity);
    35.     Instantiate(logs, tree.transform.position + Vector3(5,5,0) + position, Quaternion.identity);
    36.    
    37.     //Instantiate(coconut, tree.transform.position + Vector3(0,0,0) + position, Quaternion.identity);
    38.     //Instantiate(coconut, tree.transform.position + Vector3(2,2,0) + position, Quaternion.identity);
    39.     //Instantiate(coconut, tree.transform.position + Vector3(5,5,0) + position, Quaternion.identity);
    40.    
    41. }
    42.  
    43.  
    44.  
    And I want to reduce the health of the tree from another script, this one

    Code (JavaScript):
    1. #pragma strict
    2.  
    3. var rayLength : int = 10;
    4.  
    5. private var playerAnim : PlayerControl;
    6.  
    7. var tree : GameObject;
    8.  
    9. var tree : TreeController;
    10.  
    11. function Update ()
    12. {
    13.     var hit : RaycastHit;
    14.     var fwd = transform.TransformDirection(Vector3.forward)*10;
    15.     Debug.DrawRay (transform.position, fwd, Color.green);
    16.     //Debug.Log("Hit!");
    17.  
    18.     if(Physics.Raycast(transform.position, fwd, hit, rayLength))
    19.     {
    20.         //Debug.Log("Hit!");
    21.         if(hit.collider.gameObject.tag == "Tree")
    22.         {
    23.             //Debug.Log("Hit!");
    24.             tree = (hit.collider.gameObject);
    25.             playerAnim = GameObject.Find("FPSArms_Axe@Idle").GetComponent(PlayerControl);
    26.  
    27.             if(Input.GetKeyDown(KeyCode.Return) && playerAnim.canSwing == true)
    28.             {
    29.                 tree.GetComponent(TreeController).treeHealth -= 1;
    30.            
    31.                 Debug.Log("health");
    32.             }
    33.         }
    34.     }
    35. }
    36.  
    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
     
  2. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,187
    Use the generic version of getComponent https://docs.unity3d.com/ScriptReference/GameObject.GetComponent.html

    (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.
     
  3. Orthos30

    Orthos30

    Joined:
    Oct 24, 2017
    Posts:
    4
    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
     
  4. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,187
    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.
    Code (JavaScript):
    1.  
    2. var treeController: TreeController= tree.GetComponent.<TreeController>();
    3.  
    Similar to what you are doing.
    Code (JavaScript):
    1.  
    2. tree.GetComponent.<TreeController>().treeHealth -= 1;
    3.  
    Since the debug is working, are you getting any errors?
     
  5. Orthos30

    Orthos30

    Joined:
    Oct 24, 2017
    Posts:
    4
    not getting any errors
    with the code you gave, how would i reduce the health?
     
  6. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,187
    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?
     
  7. Orthos30

    Orthos30

    Joined:
    Oct 24, 2017
    Posts:
    4
    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