- Home /
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"));
}
}
Answer by FuzzyLogic · Feb 26, 2018 at 11:35 PM
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"));
}
}
Thanks a lot! Both of the answers said are correct. BTY, thanks for re$$anonymous$$ding me to fix my question. : )
Answer by Positive7 · Feb 26, 2018 at 11:25 PM
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);
}
Thanks so much! Worked perfectly. You just saved my old project!