Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by TommiH · Jan 28, 2017 at 10:56 AM · getcomponentnull reference exception

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

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image PizzaPie · Jan 28, 2017 at 02:32 PM 0
Share

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))]
 
avatar image TommiH PizzaPie · Jan 28, 2017 at 03:37 PM 0
Share

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).

2 Replies

· Add your reply
  • Sort: 
avatar image
2

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.

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Arshia001 · Jan 31, 2018 at 10:28 AM 1
Share

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.

avatar image jehovah0121qq Arshia001 · May 11, 2018 at 01:25 AM 0
Share

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().

avatar image Arshia001 jehovah0121qq · Feb 18, 2019 at 06:38 PM 0
Share

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.

avatar image
0

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>();
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

65 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Gameobject reference disappears at start 1 Answer

GetComponnent null reference C# 1 Answer

NullReferenceException in Two Scripts 1 Answer

Is it overkill to RequireComponent then check if GetComponent isnt Null in Awake? 1 Answer

Null Reference Exception for Animator 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges