- Home /
 
I got "Your not allowed to call this function..." in this code:
Hello, I got the error "UnityException: You are not allowed to call this function when declaring a variable. Move it to the line after without a variable declaration. If you are using C# don't use this function in the constructor or field initializers, Instead move initialization to the Awake or Start function. controlBola..ctor () (at Assets/controlBola.js:10)" in this code:
 #pragma strict
 //Fuerza aplicada al mover la bola:
 var fuerza:float=5;
 //Banderas de movimiento:
 var izq:boolean=false;
 var der:boolean=false;
 var arr:boolean=false;
 var aba:boolean=false;
 //Referencia para la camara:
 var camTransform : Transform=Camera.main.transform;
 //Direccion relativa de movimiento (depende de la camara):
 var izqRelativo:Vector3=camTransform.TransformDirection(Vector3.left);
 var derRelativo:Vector3=camTransform.TransformDirection(Vector3.right);
 var delRelativo:Vector3=camTransform.TransformDirection(Vector3.forward);
 var detRelativo:Vector3=camTransform.TransformDirection(-Vector3.forward);
 function FixedUpdate () {
     verificarTeclasPresionadas();
     verificarTeclasLiberadas();
     aplicarFuerzas();
 }
 function verificarTeclasPresionadas()
 {
     if(Input.GetKeyDown("a")||Input.GetKeyDown("left")) izq=true;
     if(Input.GetKeyDown("d")||Input.GetKeyDown("right")) der=true;
     if(Input.GetKeyDown("w")||Input.GetKeyDown("up")) arr=true;
     if(Input.GetKeyDown("s")||Input.GetKeyDown("down")) aba=true;
 }
 function verificarTeclasLiberadas()
 {
     if(Input.GetKeyUp("a")||Input.GetKeyUp("left")) izq=false;
     if(Input.GetKeyUp("d")||Input.GetKeyUp("right")) der=false;
     if(Input.GetKeyUp("w")||Input.GetKeyUp("up")) arr=false;
     if(Input.GetKeyUp("s")||Input.GetKeyUp("down")) aba=false;
 }
 function aplicarFuerzas()
 {
     if(izq)  rigidbody.AddForce(izqRelativo*fuerza);
     if(der) rigidbody.AddForce(derRelativo*fuerza);
     if(arr)  rigidbody.AddForce(delRelativo*fuerza);
     if(aba) rigidbody.AddForce(detRelativo*fuerza);
 }
 
               I really don´t know why is this because I checked the example on the offical documentation and it's this:
 // Calculate the x-axis relative to the camera
 var cam : Transform = Camera.main.transform;
 var cameraRelativeRight : Vector3 = cam.TransformDirection (Vector3.right);
 // Apply a force relative to the camera's x-axis
 rigidbody.AddForce (cameraRelativeRight * 10);
 
               Many thanks.
Answer by Eric5h5 · Jun 16, 2013 at 03:46 AM
You must not run any code outside functions. Only declare variables outside functions. The code example in the documentation is assumed to be inside a function. If you want camTransform as a global variable, then declare it outside any functions:
 var camTransform : Transform;
 
               Then run code inside a function:
 function Start () {
     camTransform = Camera.main.transform;
 }
 
               Also, do not use FixedUpdate for anything except physics. Only use Update for checking input; it will not work right in FixedUpdate since you will miss KeyDown/KeyUp events. It's better to use enums (KeyCode.A) instead of strings ("a") for input functions. Although you'd often be better off with GetButton instead of hard-coding input keys, since that won't work on all keyboards (WASD is no good on AZERTY keyboards, for example).
Your answer