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 bob_feng_2000 · Feb 27, 2016 at 03:56 PM · c#unity5logicunity4

In what situation Component.gameObject method throws NullReferenceException?

Have anybody ever met the same problem with me? When I call Component.gameObject function it throws "NullReferenceException: Object reference not set to an instance of an object" rather than "MissReferenceException: xxx".

 class A: MonoBehaviour{
 public void Func1()
 {
 Debug.Log(gameObject.name);//throw NullReferenceException here because of gameObject invoke not .name
 }
 }
 
 
 class B:MonoBehaviour{
 public static Dictionary<int, A> ADic = new Dictionary<int, A>();
 public static A AGOInScene;//initialized in somewhere and refers to a //GameObject in current scene.
 
 public void Start()
 {
 AGOInScene = xxxx;//initialized hereand refers to a //GameObject in current scene.
 ADic[7] = AGOInScene;
 }
 
 public void DestroyInDifferentWaysBeforeCallAFunc1()
 {
 case 1: ADic[7] = null;//this will lead to "Aobj.Func1();" throwing NullReferenceException
 case 2: DestroyImmediate(ADic[7]);//Nothing will happen in Aobj.Func1() but it will throw MissReferenceException inside A::Func1()
 
 So in what case will cause NullReferenceException inside A::Func1()?
 }
 
 public void CallAFunc1()
 {
 A Aobj = ADic[7];
 Aobj.Func1();
 }
 
 }


So in what case will cause NullReferenceException inside A::Func1()? Thanks in advance.

Comment
Add comment · Show 5
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 hexagonius · Feb 27, 2016 at 04:18 PM 0
Share

I don't get it. you're explicitly setting ADic[7] to null and ask when it gets null?

avatar image bob_feng_2000 hexagonius · Feb 29, 2016 at 02:15 AM 0
Share

I just want to know in what case "gameObject.name" inside of A::Func1 itself will throw NullReferenceException. Thanks for your reply.

avatar image Ali-hatem bob_feng_2000 · Feb 29, 2016 at 12:59 PM 0
Share

some times because of error & probably A Aobj = ADic[7];

Show more comments
Show more comments

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Fredex8 · Mar 01, 2016 at 06:30 AM

I'm really not entirely sure what you're actually trying to do with this as I can't follow your script very well but it looks like you're trying to do something excessively complicated to handle null references when something simple would do the job:

 if (AGOInScene!= null)
 {
      //Do something
 }

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
avatar image
0

Answer by bob_feng_2000 · Mar 03, 2016 at 07:07 AM

Thank you Fred. I found even "AGOInScene" is null because of DestroyImmediate(AGOInScene) in Coroutine, it still can went into the block //Do something. in your code except you set AGOInScene=null explicitly.

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

7 People are following this question.

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

Related Questions

How do I measure the velocity of object in Z-direction? 0 Answers

Canvas Sorting Layer having issues with Horizontal Layout Group (And override sorting is not working) 1 Answer

Why does NetworkServer.ReplacePlayerForConnection not recognize the instantiated object parameter? 0 Answers

Gizmos and Handles 1 Answer

unity3d 5 game on fullscreen shows only half of game screen 4 Answers


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