- Home /
Accessing A Variable Across Script In A Child Object
I am working on a sprinting mechanic that incorporates the use of Head-bobbing to signify the increase or decrease in motion of the character. I've attempted to try to gleam information from all tutorials and syntax I could find from the reference but I 1. Can't seem to make sense of exactly what you're doing and why (helps with future attempts) 2. Can't apply any of said methods due to my lack of experience 3. They are also of different languages but I have read around and taken precautions so that the .cs script compiles first.
Here I define variables in my "Accessed Script", called headBobbing.js which would be the Headbobbing Mechanics located on the camera of my FPController-
private float StartY; // The default local Y coordinate of a camera
private float Angle = 0; // The angle for our sinusoid
**public float BobAmount; // The maximal bobbing delta Y
public float BobbingSpeed ; // The speed with which we will change our Angle**
public CharacterController Controller;
And here is how I am attempting to implement them thus far in my "Accessing Script", called sprintCrouch.cs located on the whole of the FPController-
//Accessing HeadBobbing Script Variables
**var bobSpeed2 = GameObject.Find(GameObject).GetComponentInChildren(headBobbing).BobbingSpeed;
var bobAmount2 = GameObject.Find(GameObject).GetComponentInChildren(headBobbing).BobAmount;**
function Update ()
{
percentOfStamina = curStam/maxStam;
staminaBarLength = percentOfStamina*100;
var speed = walkSpeed;
if(charController.isGrounded && Input.GetKey("left shift") && curStam > 0 && stamCooldown == false)
{
speed = sprintSpeed;
isSprinting = true;
**bobAmount2** = .2;
**bobSpeed2** = 16;
}
else
{
speed = walkSpeed;
isSprinting = false;
**bobAmount2** = .9;
**bobSpeed2** = 12;
}