GetComponent not working for movement

Im trying to get movement working but apparently GetComponent dosent exist but i cant figure out why that is. Anybody mind helping me?

 private PlayerMotor motor;
    void Awake()
    {
        playerInput = new PlayerInput();
        onFoot = playerInput.OnFoot;
        motor = GetComponent<PlayerMotor>();
    }
    void FixedUpdate()
    {

        motor.ProcessMove(onFoot.Movement.ReadValue<Vector2>());
    }
    private void OnEnable()
    {
        onFoot.Enable();
    }
    private void OnDisable()
    {
        onFoot.Disable();
    }

Make a public field, drag it in… here’s why, and also a checklist if you want to persist in this GetComponent() craziness:

Keep in mind that using GetComponent() and its kin (in Children, in Parent, plural, etc) to try and tease out Components at runtime is definitely deep into super-duper-uber-crazy-Ninja advanced stuff.

Don’t use all those Get/Find things unless you absolutely MUST use them. And when you do decide you’re ready to do it, here’s the bare minimum of stuff you absolutely MUST keep track of if you insist on using these crazy Ninja methods:

  • what you’re looking for:
    → one particular thing?
    → many things?
  • where it might be located (what GameObject?)
  • where the Get/Find command will look:
    → on one GameObject? Which one? Do you have a reference to it?
    → on every GameObject?
    → on a subset of GameObjects?
  • what criteria must be met for something to be found (enabled, named, etc.)
  • if your code expects one instance and later you have many (intentional or accidental), does it handle it?

If you are missing knowledge about even ONE of the things above, your call is likely to FAIL.

If you have issues, start debugging. We know the above methods will ALWAYS work so find out what you’re doing wrong before bothering to make a forum post.

This sort of coding is to be avoided at all costs unless you know exactly what you are doing.

Botched attempts at using Get- and Find- are responsible for more crashes than useful code, IMNSHO.

If you run into an issue with any of these calls, start with the documentation to understand why.

There is a clear set of extremely-well-defined conditions required for each of these calls to work, as well as definitions of what will and will not be returned.

In the case of collections of Components, the order will NEVER be guaranteed, even if you happen to notice it is always in a particular order on your machine.

It is ALWAYS better to go The Unity Way™ and make dedicated public fields and drag in the references you want.

Does your class inherit from MonoBehaviour?

no i saw now that it wasnt but ive decided to try to find easyer ways of doing this

Make a public field, drag it in… here’s why, and also a checklist if you want to persist in this GetComponent() craziness […]

I’m not sure that this is the right advice! GetComponent is usually taught to beginners. In most beginner applications it’s straightforward to use and debug. (Case in point: another reply diagnosed and solved this problem in only a few words.)

There’s certainly a nuanced conversation to be had about the advantages and disadvantages of this approach versus assigning references in the Inspector, but ‘avoid GetComponent at all costs’ isn’t really it.

Most ninja coders avoid making a variable public unless it absolutely needs to be. This will make your code much easier to understand and debug. You can still make things appear in the Inspector easily by replacing public int myInt with [SerializeField] private int myInt.

Last nitpick, I promise! I think we should be wary of conflating the ‘Get’ methods with the ‘Find’ methods. GetComponent and similar methods are relatively performant and (just like you say) they are used readily by more experienced developers. By contrast, FindObjectOfType and similar methods are quite performance-heavy, and used by experienced developers very sparingly or not at all. The two are quite different.

Don’t be scared of GetComponent!