NullReferenceException from within GetComponent
Hello,
I seem to be getting a NullReferenceException using the GetComponent function. This is not a case of GetComponent returning a null value, which would happen if the component didn't exist on the game object. It's literally from the GetComponent function. Stack trace from a build (without developer mode so line numbers aren't visible):
NullReferenceException
at (wrapper managed-to-native) UnityEngine.Component:GetComponent (System.Type)
at UnityEngine.Component.GetComponent[PhotonView] () [0x00000] in <filename unknown>:0
at PhotonView.Get (UnityEngine.Component component) [0x00000] in <filename unknown>:0
at Photon.MonoBehaviour.get_photonView () [0x00000] in <filename unknown>:0
at CombuMultiplayerRelay.GrantAchievement (System.String achievementId, Single percentageAdded, .CreatureFSM player) [0x00000] in <filename unknown>:0
Does anyone know what could possibly cause this? Is it a bug in Unity? I don't think it should be possible to get an exception from a Unity API function like this.
My code just tries to access Photon's "photonView" component and send an RPC.
Edit: We're on 4.7.2f1
Well GetComponent can't throw that exception because it is set to return null in case of the component is not there. The exception is thrown by any statement that tries to access that component after the GetComponent. Now why wouldn't it return the component even it s there can be caused by several reasons as , if you use GetComponent(string) you might misspell the string , the GameObject is inactive and so on. You should always have a check after GetComponent as SomeComponent someComponent = gameObject.GetComponent(typeof(SomeComponent)) as SomeComponent; if(someComponent == null) return;
or on a script attached on the GameObject where SomeComponent should always be you can use
[RequireComponent(typeof(SomeComponent))]
I know it's supposed to return null if the component isn't there. But as you can see in the error message, the NullReferenceException is happening inside GetComponent. So either the error message itself is faulty (maybe because it's developer mode), or something goes wrong with GetComponent. Which is why I wonder if this is a Unity bug (and have submitted a bug report).
Answer by TommiH · Feb 06, 2017 at 04:58 PM
I figured it out. This is what you get if you try to call GetComponent on a game object that has been destroyed. It only happens like this in a build - in the editor you get a much more helpful error:
MissingReferenceException: The object of type 'GameObject' has been destroyed but you are still trying to access it.
This seems to be the case in Unity 5 still.
I'm getting the same exception, except in my case, I think it's caused by accessing an object before it's created... I don't know how that's even possible though.
Guess not. When a game object is created, the $$anonymous$$B scripts, if enabled, will be waked up, and you can call GetComponent() on it even in Awake().
How about adding components in with scripts? I know Awake
is called during AddComponent
, but Start
is called after the current script event is finished. I don't think getting references to stuff (specially on the same GameObject
) is safe during Awake
.
Answer by LemonCurry · Feb 18, 2019 at 02:51 PM
I had the same error and thought I'd post an answer here since this is the first thing that comes up in google. My code was
if (x != null)
var y = x.GetComponent<Y>();
And I changed it to
if (x.gameObject != null)
var y = x.GetComponent<Y>();