- Home /
Changing the parent's position based on a child's variable
This is the code for my object "Light Switch" attached to an object called "Floor" which is parented to an object called "ShadowWall"
var hit : RaycastHit; var keyCode : KeyCode; var state : int = 1;
function Update () {
if (Input.GetKey (KeyCode.X)){ if (Physics.Raycast (transform.position, new Vector3(0,-1,0), hit, 10) ){ if (hit.collider.gameObject.tag=="Ninja") { if (state==1){ state=1; } if (state==2){ state=1; } } }
}
if (Input.GetKey (KeyCode.Return)){ if (Physics.Raycast (transform.position, new Vector3(0,-1,0), hit, 10) ){ if (hit.collider.gameObject.tag=="Samurai") { if (state==1){ state=2; } if (state==2){ state=2; } } } }
}
I wish to have it so that when the state of "Floor" (the script above is inside the object Floor) is 1, then ShadowWall (the parent) will be positioned right under and behind it. When the state is turned to 2, it will move shadow wall to a position outside of the screen. When changed back to 1, then shadowwall will return to it's position directly behind and under floor.
What's the code I need to use to have ShadowWall call on and check the "State" within it's child's script?
Or would it be easier if ShadowWall were a child of Floor?
edit: This is the coding for the ShadowWall I'm trying to use.
function Update () { var script : Lightswitch; script = GetComponentInChildren(Lightswitch); script.state ();
if (child.state=1){ transform.position.x =830 transform.position.y =2.5 transform. position.z =58.36104 }
if (child.state=2){ transform.position.x =830 transform.position.y =-1000 transform. position.z =58.36104 }
}
How do I get it too call upon the object? please? Really need the help :( I keep getting this message:
Assets/Standard Assets/Scripts/Shadows.js(5,8): BCE0077: It is not possible to invoke an expression of type 'int'.