- Home /
How to call a variable that was defined in FixedUpdate in javascript
I have this written up in FixedUpdate:
function FixedUpdate(){
var velController : CharacterController = GetComponent(CharacterController);
var horizontalVelocity : Vector3 = controller.velocity;
horizontalVelocity = Vector3(controller.velocity.x, 0, controller.velocity.z);
//The speed on the x-z plane
var horizontalSpeed : float = horizontalVelocity.magnitude;
I want to be able to call the horizontalSpeed variable from within a OnTriggerEnter like this:
function OnTriggerEnter (col : Collider){
if(horizontalSpeed <= 1 && col.gameObject.tag == "Turret" )
{
Debug.Log("KABOOM");
}
}
I can't put the OnTriggerEnter inside the FixedUpdate, and I get an UnknownIdentifier error leaving it as is. I know this is probably more stackexchange worthy because is a generic programming thing more than a Unity specific thing, but I tried looking around there and googling "javascript + how to call a variable inside another function" and got nothing I could use. If you could help me despite that fact I would really appreciate it.
Answer by Mikael-Gyth · May 01, 2014 at 07:05 AM
The scope of the variable is limited to the method you defined it in. Simply define the variable in the class/outside fixedupdate, and you can access it from both methods.
Your answer
![](https://koobas.hobune.stream/wayback/20220613144007im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How do you achieve variables and functions that are global between scenes? What is the BEST way? 1 Answer
Error on my javascript code 1 Answer
How to access variable from another function? 2 Answers
Lower player's health from separate script. 2 Answers
Javascript Movement Script. Why am I getting these errors? 2 Answers