Error: GetComponentFastPath is not allowed?

BTY thanks in advance! So i’m getting this error saying:

GetComponentFastPath is not allowed to be called from a MonoBehaviour constructor (or instance field initializer), call it in Awake or Start instead. Called from MonoBehaviour ‘KingCubisAPP’.
See “Script Serialization” page in the Unity Manual for further details.
UnityEngine.Component:GetComponent()
KingCubisAPP:.ctor() (at Assets/Scripts Folder/KingCubisAPP.js:3)
UnityEngine.GUIUtility:ProcessEvent(Int32, IntPtr)

I know the code is sloppy, but it was working before on a previous version of Unity. Does anyone know what is wrong? Here’s the java code that I use…

#pragma strict

private var animator = GetComponent.<Animator>(); 
public var applyRootMotion;
 //var yourAnimation : Animation; // choose the animation which will start when triggered
 var FallerObject : GameObject; //Implement your Faller game object into this variable in the inspector
 //var After6 : GameObject;

 function Start (){
 animator.GetComponent(Animator);
 }
    
 function OnTriggerStart (Other : Collider){
  
 if(Other.gameObject.tag == "Player"){
          yield WaitForSeconds(1);

 FallerObject.GetComponent.<Animator>().Play("Up");
GetComponent.<Animator>().applyRootMotion = false;
animator.applyRootMotion = false;

yield WaitForSeconds(2);
Destroy (GameObject.FindWithTag("After6"));

}
}

The problem is with the line:

private var animator = GetComponent.<Animator>();

You cannot (usually) call a function when declaring a class variable because there’s no guarantee that the Animator component exists yet at that time.

You need to assign the value to animator from the Start() or Awake() function as described in the error.

Something like:

 #pragma strict
 
private var animator; 
public var applyRootMotion;
//var yourAnimation : Animation; // choose the animation which will start when triggered
var FallerObject : GameObject; //Implement your Faller game object into this variable in the inspector
//var After6 : GameObject;
 
function Start (){
    animator = GetComponent.<Animator>();
}
     
function OnTriggerStart (Other : Collider){
   
    if(Other.gameObject.tag == "Player"){
        yield WaitForSeconds(1);
 
        FallerObject.GetComponent.<Animator>().Play("Up");
        GetComponent.<Animator>().applyRootMotion = false;
        animator.applyRootMotion = false;
 
        yield WaitForSeconds(2);
        Destroy (GameObject.FindWithTag("After6"));
 
    }
}

You can’t use GetComponent in field initializer.

change private var animator = GetComponent.<Animator>();
to var animator : Animator;
and in Start() :

function Start() 
{
    animator = GetComponent(Animator);
}