How to read variable from parent object?

Sorry if this is an exceedingly simple question; somehow haven’t been able to find an answer.

I have a parent object, “TankChassis”, with the script “Movement” (javascript) that contains a variable “directionAngle” that describes the angle it is facing (y component.) The tank chassis is driven around with WASD.

Within TankChassis I have “TankTurret”, with the script “TurretControl” that rotates the turret between -110 and +110 degrees (eulerAngles.y) depending on the x position of the mouse.

What I want to do is add directionAngle from TankChassis to the angle of the turret so that the turret rotates in relation to the tank chassis.

What’s the best way to do this? Tl;dnr - how do I get a child’s script to read a variable from a script in its parent?

(I’m learning Unity by brute force; hope I’m not bothering anyone with simple questions!)

Thank you so much!

(A picture of my cute, derpy tank for your trouble: )

Edit: moved from comments, really more of an answer i guess

must be taking direct input off of the mouse position, in which case… if you really want to, you could add a var and pass the difference of the tanks rotation to it…

offset = currentTankRotation - originalTankRotation ;
turret.rotation = mousePositionX + offset; //pseudocode

oh,and as for how to read the var,you could just say:

    var tank : Transform;
    tank = transform.parent;

   var  tankYRotation = tank.eulerAngles.y; //for example

once you have that, you can access other scripts you’re after too, with

var myScript = tank.gameObject.GetComponent(MyScript); // MyScript = the name of the script
    myScript.hitPoints = 100; // var (of type int) on myScript
    myScript.DoSomething(); // function on myScript


one good way would be to just use the distance of mouse movement to apply the rotation:

var lastMousePos : float;
var currentMousePos : float;

function Start()
currentMousePos = Input.mousePosition.x;
lastMousePos = Input.mousePosition.x;

function Update(){
currentMousePos = Input.mousePosition.x;
var thisFrameRotate = currentMousePos.x - lastMousePos.x;
transform.Rotate(Vector3.up, thisFrameRotate);
lastMousePos = Input.mousePosition.x;

not tested but should work, at least to give an idea

and glad to help!

Well from what I gather from your post, you could do something like:

 var movementScript = transform.parent.GetComponent("Movement");
 turretAngle = movementScript.directionAngle;