Error CS0103: The name 'GetComponent' does not exist in the current context. What do I need to do?

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class animposit : StateMachineBehaviour
{
// OnStateEnter is called before OnStateEnter is called on any state inside this state machine
//override public void OnStateEnter(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
//{
//
//}
// OnStateUpdate is called before OnStateUpdate is called on any state inside this state machine
//override public void OnStateUpdate(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
//{
//
//}
// OnStateExit is called before OnStateExit is called on any state inside this state machine
//override public void OnStateExit(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
//{
//
//}
// OnStateMove is called before OnStateMove is called on any state inside this state machine
//override public void OnStateMove(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
//{
//
//}
// OnStateIK is called before OnStateIK is called on any state inside this state machine
//override public void OnStateIK(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
//{
//
//}
// OnStateMachineEnter is called when entering a state machine via its Entry Node
//override public void OnStateMachineEnter(Animator animator, int stateMachinePathHash)
//{
//
//}
// OnStateMachineExit is called when exiting a state machine via its Exit Node
//override public void OnStateMachineExit(Animator animator, int stateMachinePathHash)
//{
//
//}

Animator waAni;

void Start ()
{
waAni = GetComponent ();
}
void Update()
{
int currentState = waAni.GetInteger(“statev”);
if (currentState == 0)
{
}
if (Input.GetKey(KeyCode.S))
{
waAni.SetInteger(“statev”, 1); // down
}
else if (Input.GetKey(KeyCode.D))
{
waAni.SetInteger(“statev”, 2); // right
}
else if (Input.GetKey(KeyCode.W))
{
waAni.SetInteger(“statev”, 3); // up
}
else if (Input.GetKey(KeyCode.A))
{
waAni.SetInteger(“statev”, 4); // left
}
}
}

Look at the docs: Unity - Scripting API: StateMachineBehaviour

State machine behaviours are scriptable objects, not components. You need a reference to a game object or a component to in order to try and get references to its components.

EDIT: All the state machine behaviour calbacks pass the parameters you need anyway. I’m not sure why you’re trying to treat this like a normal monobehaviour.

How do I reference components?

All the state machine methods, as mentioned, pass a reference to an animator component. So if that’s all you need, then you don’t need to get the component at all.

If you need another component, you get it via the animator reference you already have.

And if it wasn’t clear, neither Start or Update WILL NOT be called here because StateMachineBehaviour is not a monobehaviour, it’s a scriptable object.

Ok thx

I think you just mistyped MonoBehaviour and it gave you StateMachineBehaviour at the top when it spell corrected (which is called Intellisense)

Go change out the word StateMachineBehaviour near the top and instead put MonoBehaviour… I’m almost 100% certain that’s what you intend.

In any case, check back with where you’re working from:

Tutorials and example code are great, but keep this in mind to maximize your success and minimize your frustration:

How to do tutorials properly, two (2) simple steps to success:

Step 1. Follow the tutorial and do every single step of the tutorial 100% precisely the way it is shown. Even the slightest deviation (even a single character!) generally ends in disaster. That’s how software engineering works. Every step must be taken, every single letter must be spelled, capitalized, punctuated and spaced (or not spaced) properly, literally NOTHING can be omitted or skipped.
Fortunately this is the easiest part to get right: Be a robot. Don’t make any mistakes.
BE PERFECT IN EVERYTHING YOU DO HERE!!

If you get any errors, learn how to read the error code and fix your error. Google is your friend here. Do NOT continue until you fix your error. Your error will probably be somewhere near the parenthesis numbers (line and character position) in the file. It is almost CERTAINLY your typo causing the error, so look again and fix it.

Step 2. Go back and work through every part of the tutorial again, and this time explain it to your doggie. See how I am doing that in my avatar picture? If you have no dog, explain it to your house plant. If you are unable to explain any part of it, STOP. DO NOT PROCEED. Now go learn how that part works. Read the documentation on the functions involved. Go back to the tutorial and try to figure out WHY they did that. This is the part that takes a LOT of time when you are new. It might take days or weeks to work through a single 5-minute tutorial. Stick with it. You will learn.

Step 2 is the part everybody seems to miss. Without Step 2 you are simply a code-typing monkey and outside of the specific tutorial you did, you will be completely lost. If you want to learn, you MUST do Step 2.

Of course, all this presupposes no errors in the tutorial. For certain tutorial makers (like Unity, Brackeys, Imphenzia, Sebastian Lague) this is usually the case. For some other less-well-known content creators, this is less true. Read the comments on the video: did anyone have issues like you did? If there’s an error, you will NEVER be the first guy to find it.

Beyond that, Step 3, 4, 5 and 6 become easy because you already understand!

1 Like