- Home /
An instance of type 'UnityEngine.Animator' is required to access non static member 'GetBool'
I'm working on a FPS. I'm new in Unity so I don't know much about scripting. I want to make a script that makes the player able to enter aiming mode. This is the script:
@script RequireComponent(Animator)
function Update () {
if (Input.GetButtonDown ("Fire2")) {
if (!Animator.GetBool("aimingMode")) {
Animator.SetBool("aimingMode", true);
aimingMode = true;
}
}
}
I know it's unfinished. The problem is that when I take a look at the console in unity, it says:
An instance of type 'UnityEngine.Animator' is required to access non static member 'GetBool'.
I don't know how to solve this. The gameobject has already got an animator with an animation controller and the booleans set. Please help.
Answer by Professor Snake · Mar 21, 2014 at 03:47 PM
Animator by itself is a class, there is no logic in calling GetBool on it. You need to reference the Animator component on your gameobject. So, that would be
var animator:Animator;
function Start(){
animator=GetComponent(Animator)
}
function Update () {
if (Input.GetButtonDown ("Fire2")) {
if (!animator.GetBool("aimingMode")) {
animator.SetBool("aimingMode", true);
aimingMode = true;
}
}
}
When I press Fire2 nothing happens. In the animator I've got a default state with no animation. Then there is a transition to the ai$$anonymous$$g state that happens when ai$$anonymous$$g$$anonymous$$ode is true. After that I've got a transition (that happens when ai$$anonymous$$g$$anonymous$$ode is false) to another state, which has the same animation as the previous state, but with speed -1 so the gun returns to its original position. Finally there is a transition to the first state (the one with no animation) with the exit time condition. I have very little experience with unity and I don't know what's wrong.
EDIT: Sorry, I made a big mistake. It's working now.
Your answer
![](https://koobas.hobune.stream/wayback/20220613140827im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
2D Animation does not start 1 Answer
Modifying components of an array 1 Answer
NullReferenceException when trying to animate sprite 0 Answers
Animator problem start animation 1 Answer