• 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
Question by Ambrose998800 · Jan 12, 2017 at 03:12 AM · error messageonenable

OnEnable called by disabled object

I just run into the problem, that it seeems the function OnEnable() is called by not enabled objects what causing my script to crash.

Here is the code that produces the following error:

NullReferenceException: Object reference not set to an instance of an object

Code:

  void OnEnable()
     {
         CalculatePositions();
         print("CalculatePositions() by OnEnable: " + transform.parent.parent.name); //ERROR HERE
     }

It seems, the error is produced by accessing the name of the parent.parent of the code holding object. The parent.parent exists as you can see in following picture:

alt text

But is not active (like the object that produces the error message).

Could it be, that the OnEnable() function is called when instantiate an inactive object, causing this problem? If so, how can I work around that?

onenableproblem.jpg (91.5 kB)
Comment
IARI

People who like this

1 Show 6
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 Pengocat · Jan 12, 2017 at 03:53 AM 1
Share

Strange. You can check if the parent is active before you use it like this transform.parent.parent.gameObject.activeInHierarchy it should prevent the null error if it gets called at some odd time. When you create an inactive instance OnEnable should not be called unless if you disable it after you instantiated it.

avatar image fafase · Jan 12, 2017 at 06:29 AM 1
Share

You should add the CalculatePositions method. The error may not be the printing.

avatar image Ambrose998800 fafase · Jan 12, 2017 at 07:44 AM 0
Share

I just deleted the CalculatePositions() for testing, the error is the print line... And that OnEnable() is called by a disabled object.

avatar image LK84 Ambrose998800 · Jan 12, 2017 at 01:03 PM 0
Share

is your prefab disabled or do you disable it after instantiation. Because OnEnable() never gets called from a disabled or inactive object!!!

Show more comments
avatar image hexagonius · Jan 13, 2017 at 09:06 AM 0
Share

The moment a gameobject is instantiated from an "active" prefab, any script with Awake and OnEnable get that called immediately. This means that no code related to parenting has been executed and therefore there's not even a single parent.
You can either block the first OnEnable call from happening with a boolean check called "init" which get's set to true in Start. Then, the second time OnEnable is called it will pass and execute.
The other option is to make sure to disable the prefab or gameobject, whatever you're creating the instantiation of, prior to doing that.

0 Replies

· Add your reply
  • Sort: 

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

63 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

Related Questions

the namespace 'Global::' already contains a definition for 'Chunk' 1 Answer

how do i fix: NullReferenceException: Object reference not set to an instance of an object CityGenerator.CreateCity () (at Assets/materials/city/city generator.cs:64) 1 Answer

FATAL ERROR: trying to write to readonly database 0 Answers

My Unity is buggy after 5.6.0 update 1 Answer

Cannot paint textures onto terrain, errors are raised 0 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