- Home /
Acces to a var from another Script.
Hi everyone. I did 1 script to torque an object when i use the keyArrows. But then i want to use that torque in a second object and use de axis Z from the first to apply a force. But i dont know how to access to that var. Can you help me?
This is the Torque Script
#pragma strict
function FixedUpdate()
{
var rotateHorizontal:float = Input.GetAxis ("Horizontal");
var torsion:Vector3 = new Vector3(0.0f, rotateHorizontal, 0.0f);
rigidbody.AddTorque(torsion);
}
And this is the second Script which i want get access to the first script var.
#pragma strict
var moveReferenceScript: MovementReference;
function FixedUpdate()
{
rigidbody.AddForce(moveReferenceScript.torsion * 100);
}
But obviously, this doesnt work, any idea? Thank you.
Answer by robertbu · Feb 20, 2014 at 03:35 PM
Assuming your first script is named 'MovementReference', and assuming you are planning to initialize 'moveReferenceScript' in the inspector, then the only issue left is scoping. That is, in the first script you declare 'torsion' inside of FixedUpdate(). The result is 1) the variable is not available at the class level, and 2) it only exists during the duration of the FixedUpdate() call. At the top of the file put:
var torsion : Vector3;
Then in FixedUpdate() the line becomes:
torsion = new Vector3 (0.0f, rotateHorizontal, 0.0f);
Answer by AnxoDL · Feb 20, 2014 at 03:51 PM
Now i can access to that ty. But i cant do that it works in the second sphere xD.
Your answer
Follow this Question
Related Questions
Setting Scroll View Width GUILayout 1 Answer
Scripts accessing one another (JS) 0 Answers
Can someone help me fix my Javascript for Flickering Light? 6 Answers
Accessing Script From Other Script Causes Lag? 1 Answer
Changing variables in GUI? 0 Answers