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 /
avatar image
0
Question by swanijam · Dec 05, 2018 at 06:51 PM · transformdisableheirarchy

In need of an OnWillDisable() event

So have situation where i my camera is the child of an object that might be disabled. So, when that object is disabled, I attempt to reparent the camera to the next appropriate parent/camera-holder.

However, when editing the transfrom heirarchy during OnDisable, you get this error:

 Cannot change GameObject hierarchy while activating or deactivating the parent.

which is problematic.

I do want this to specifically occur when the parent gameObject has its activeSelf set to false, and that doesn't necessarily coincide with any other objects getting their activeSelf set to true, so I can't very well move this to OnEnable on another object.

I also can't just choose to wait a frame before the heirarchy change, because that would require the camera to deactivate for at least 1 frame. Part of the motivation for this system is keeping a camera alive when moving between scenes and positions.

It would be perfect to have an OnPreDisable() event, but alas. That doesn't seem to be available. I would need to know every place in my code that the parent might be disabled, and run OnPreDisable() then, which is unideal, and also makes it so i can't test this with the active-checkbox in the editor.

this is a similar problem to this post: https://answers.unity.com/questions/505088/unity4-change-hirachy-ondisable.html but the solutions offered here do not apply well to my situation.

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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Vega4Life · Dec 05, 2018 at 07:22 PM

I would just create a simple extension method. Something like this:


 public static class ExtensionMethods
 {
     public static void PreDisableCameraChange(this GameObject go, Transform newParent)
     {
         Camera.main.transform.SetParent(newParent);
         go.SetActive(false);
     }
 }


Then instead of doing gameObject.SetActive(false); - you do this instead:


 gameObject.PreDisableCameraChange(newParentTransform);


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 surfuay · Dec 05, 2018 at 07:51 PM

what If you had 2 cameras and both responded to the disable function of the first parent.

if parent 1 is enabled camera1.setactive(true) and camera2.setactive(false)

and then if parent 1 is disabled do the inverse. I would put camer2 under parent 2 and you'll just have some script communication to write out.

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

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

deactivate/activate child/separate prefab from script attached to other object 2 Answers

Disable/Hide transform 4 Answers

Child of Parent 1 Answer

Disabling child objects with their own child objects 1 Answer

mesh collider won't disable when using transform on his parent 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