Unity 6 GetComponent Not Getting Components

I’m pretty new to Unity but figured I would give v6 a go since I like the improvements in some areas for performance, but I’m a little perplexed on “GetComponent” as it seems to behave different now in a MonoBehavior script.

Usage Scenario:

Assign the var "anim" the reference to the Animator component in Awake:

[SerializeField] private Animator anim;
private void Awake() {
anim = GetComponent<Animator>();

}

No compiler errors, always returns Null Reference. Only when I assign it manually in the inspector do these null references stop.

Background: I started a new project in Unity 6 (6000.0.32f1 LTS), tried the above code like I have on 20+ other scripts in the past, and I’m getting null references. These null references stopped after I manually assigned the component to anim in the inspector. Rest of the code was working fine after that.

Since it’s early I tested this:
Saved the project and closed it. Switched to 2022.3.16f1 LTS. Battled your usual headaches that come up when changing Editor versions (removing packages not avail in 2022, etc.). I made a single change in my scripts because “velocity” is actually velocity in 2022 (its depreciated in Unity 6 for linearVelocity), but nothing related to “GetComponent”, and it worked like always.

My machine: Brand new MSI laptop on Windows 11 (24H2 Build 26100.2605) (purchased Dec 22, 2024), fresh setup (no backed up data at all), new install of VS2022, new install of Unity 6 (and then 2022.3 after for testing in a known good environment). (if anyone says Bad Unity 6 install maybe Unity should remove it from LTS if the very first install I do of it gets corrupted - and the chances of the computer doing it is pretty slim - unity is the only thing “acting up” as end users put it)

The same method above is what I’ve done in 2019, 2020, 2022, 2023…and it worked but not U6.

I’ve read over the docs from the site and I’m still stumped:
Unity 6 GetComponent:
https://docs.unity3d.com/6000.0/Documentation/ScriptReference/GameObject.GetComponent.html

Unity 2022.3 GetComponent:
https://docs.unity3d.com/2022.3/Documentation/ScriptReference/GameObject.GetComponent.html

Perhaps I’m missing something being a Unity n00b but should GetComponent not work the same across everything? Go easy on me :smiley:

Mods: if this is in the wrong section please move it to the right one

Hey, I’m new to Unity too, but I think the issue might be that Awake is running very early in the initialization process, which can sometimes cause issues. You could try moving the GetComponent call to the Start method instead, since Start runs after all Awake methods are finished and components are more likely to be ready. If GetComponent is still returning null, it might be worth checking if this is a Unity 6-specific issue or a bug. Hope this helps!

GetComponent is definitely working correctly and getting components in Unity 6. It is a very basic method to not work correctly.

Does the game object in Unity Editor that has this script, also have an Animator component? If not that’s why you get null.

When you are saying you are assigning it manually in the inspector, do you drag & drop the game object that has this script or another game object with an Animator component?

The only way that this will return null with the GetComponent the way you have written it, is if the game object that has this script doesn’t also have an Animator component.

This doesn’t affect GetComponent at all, in fact the Awake is the best place to use it.

2 Likes

Oh I’m sure it’s working just peachy - I’m almost convinced it’s a me problem lol

I used Animator because I was typing between doing things and that’s what kept popping in my head, it didn’t matter the component - but for your question the GameObject, RigidBody2D(my actual component) and script were on the same object.

My script starts off as follows:

[SerializeField] private Rigidbody2D _rb;

private void Awake(){
   _rb = GetComponent<Rigidbody2D>();

}

When I say “assign them in the inspector” I make the variable visible in the inspector (either public declaration or with [SerializeField]) and drag the reference into the slot in the inspector (in this case for the RigidBody2D) (in this case it’s the same game object where the script is attached). (I also work in IT so we sometimes have our own language we refer to things as)

This problem completely disappeared when I flipped back to 2022.3 and the only line I changed was out of necessity due to a change in the RigidBody2D reference to velocity between Unity versions
From:
_rb.linearVelocity = 0f;

To
_rb.velocity = 0f;

Absolutely 0 changes in Unity itself to the GameObject setup, just my Editor Version (had I think 2 or something packages I had to take out because they weren’t released for 2022.3)

I’m posting the question to fix my understanding of it so I can do better in the future (I’m learning on my own with no help from anyone because I’m a foolish person…). Reading back it looks like I’m blaming Unity (as a good user always thinks: It’s definitely not me it must be the tool!), which might be accurate if that’s actually the case, but if my syntax is wrong or I’m using “GetComponent” wrong I want to correct it now so I can start using Unity 6 (I like the right click menu better than other editor versions)

Let’s leave the “assign them in the inspector” for now, because it works as expected and focus on the GetComponent.

In Unity6, in a new project in a a new scene, with a game object that has only the transform, the Rigidbody2D and a script that only contains:

private Rigidbody2D rb;
   
   private void Awake()
   {
      rb = GetComponent<Rigidbody2D>();
      if(rb == null) 
         Debug.Log("rb is null");
      else
         Debug.Log("rb is not null");
   }

when you press play, you should see in the console rb is not null, isn’t that the case for you?

1 Like

And that’s why we assign things in the inspector when we can: it works.

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.

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.

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.

1 Like