Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
avatar image
4
Question by FriedrichWessel · Jul 31, 2013 at 06:25 PM · unity 4.2

Unity4: Change Hirachy OnDisable

Hello,

I just upgraded from Unity3.5 to Unity 4.2 and I get Problems with my Objectpools. The Objects in the Games are pooled for Memory and LoadingTime Optimisation.

Until now the Pool was working like this ( just the important parts for the Problem ) :

  class ObjectWithPooledMembers{
     
    void Start(){
     pooledGameObject = Pool.Instance.Instatiate(pooledPrefab); 
     pooledGameObject.transform.parent = gameObject.parent; 
    }
    void OnDisable()
    {
       Pool.Instance.Deactivate(pooledGameObject); 
     }
     
     class Pool(){
      public void Deactivate(GameObject object)
      {
        // this is no longer allowed in Unity4 - because its called from inside OnDisable
        object.transform.parent = Pool.Instance.gameObject.transform;  

        object.SetActive(false); 
      }
     }
 

 

But now I`m no longer allowed to do this change OnDisable ( Error: Cannot change GameObject hierarchy while activating or deactivating the parent. )

Is there any function that is called before the Parent is realy Disabled ? Or Any other Idea how to implment a Pool in Unity4 if you need to change the Hirachy on Enable / Disable ?

Thanks for your help!

Friedrich

Comment
Add comment
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

3 Replies

· Add your reply
  • Sort: 
avatar image
3

Answer by POiD · Mar 18, 2015 at 08:15 PM

Came across this Question when searching for a solution my problem with Object Pools and returning items to the pool when I change scenes. I was having the exact same issue with OnDisable, but not for all my objects, which confused me.

Turns out the objects I had no issues with were controlled and tracked by the script, but when I got them from the pool I was setting their Parent object to be a child of the GameObject my script was on. When in OnDisable, they could successfully be returned to the Pool without issue.

Thus to solve my issue, I slightly changed how I was using the UI components I was using from my Pool and placed them on a Empty Child GameObject. This still allowed me to release them to the pool using OnDisable.

Before: Panel (with Script) -> PooledObject

After: Panel (with Script) -> Empty GameObject --> PooledObject

Give this a try if you have similar issues. Needs slight reworking of your scene, but it works.

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 Xtro · Jul 31, 2013 at 07:45 PM

As far as I know, you can't activate a child object if its parent is deactivated. But there is an internal state of being active even if the parent is deactivated.

Please see this link : http://docs.unity3d.com/Documentation/ScriptReference/GameObject-activeSelf.html

Comment
Add comment · Show 4 · 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 FriedrichWessel · Jul 31, 2013 at 08:41 PM 0
Share

Thank you fpr the quick answer. But the Problem is not the activation or deactivation. The Problem is, that I can not change the hierachy of a tree, where there parent is going to disable. $$anonymous$$eaning: if the Parent is deleted - it first gets disabled ( and OnDisable is called ), and inside the OnDisable function I´m not allowed to unparent the children and put them back in the pool. So I need a function that is called in the process of deletion - but before OnDisable - or a total new idea for handling pooled objects.

avatar image imtrobin · Oct 30, 2013 at 04:36 PM 0
Share

I just upgraded my object pool code to U4, and faced similar problems. This change is plain stupid. Why does it need to prevent reparenting when disabled?

avatar image Xtro · Jan 30, 2014 at 09:00 PM 0
Share

Can you please mark the correct answer if you fixed that problem?

avatar image Friedrich_Wessel · Jan 31, 2014 at 10:22 AM 1
Share

Unfortunatly there is no "correct" answer. Basicly you cannot do this easy implementation of a pool any longer in U4. You have to re-write the whole system. So OnDisable gets never called - you have to detect yourself that the GO is going to be destroyed, and start your own pool logic.

avatar image
0

Answer by angelonit · Oct 28, 2021 at 06:28 AM

What I ended up doing was saving the references to the gameobjects I wanted to change parents to in a list (from OnDisable() ) and parent them from the list afterwards.

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

20 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

Related Questions

Old Scene still visible after Loading new 0 Answers

Navmesh Unity Free 0 Answers

Augmented Reality and Location 1 Answer

Importing Animated Fbx into Unity 1 Answer

Unity closes when some object and added 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