• 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 /
avatar image
0
Question by travispettry · Apr 30, 2019 at 12:58 AM · nullreferenceexception

MonoBehaviour transform is null

Hi All,

I have a bug in my game that I cannot reproduce to debug. I am hoping you all can help point me in a good direction.

I have one beta tester who keeps getting a null reference exception. The object in question is created at the start of the game, the object is a level marker. When the user completes a level I enable the stars on the map for the completed level. The user is gettting a null refreerance exception when I call transform.Find("somePath");. Is there any reason why the transfrom of a MonoBehaviour would become null?

Thank you,

Travis Pettry

Comment
Add comment · Show 3
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 MasterChameleonGames · Apr 30, 2019 at 02:23 AM 0
Share

Is there actually a child of that gameobject called "somePath"? The Find() part is likely returning an error.

avatar image travispettry · Apr 30, 2019 at 10:30 AM 0
Share

Yes the path does exist. I am only having this error occur with one beta tester.

avatar image MasterChameleonGames travispettry · Apr 30, 2019 at 10:17 PM 0
Share

If absolutely no one else has this issue, you might just want to ignore it.

Otherwise (or if you don't want to), check that it is never deleted during runtime.

Did the tester record the gameplay? You could try to do exactly what was done to try to reproduce the error and go on from there.

You could also just have another way to get somePath since Find() isn't the best option.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by junaid109 · Apr 30, 2019 at 01:31 PM

It is a bad idea to use the Find() method as it will not give you a clear idea of where your game is breaking. A null is returned as it cannot find the object you want it to find. Have you checked the name of the object at runtime etc?

Also, take a look at the docs it says "Find does not perform a recursive descend down a Transform hierarchy."

https://docs.unity3d.com/ScriptReference/Transform.Find.html

You should keep a track of objects at start time perhaps as a pool maybe. Then you can always find the object you are looking for unless it's destroyed. However, if you are only creating or using a few objects just create an array of objects from some SpawnManager class for example.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

108 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 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

NullReferenceException error in an array of objects 0 Answers

NullReferenceException when passing variable to instantiated object 1 Answer

2d raycast null 2 Answers

NullReferenceException on initialized static variable? 1 Answer

NullReferenceException when passing a gameObject as a parameter 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