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 Cyberdogs7 · Sep 27, 2012 at 08:41 PM · unity4

Unity4 .Active replacement

I am upgrading to unity4 from 3.5 and I am getting warnings from the changed behavior on the .Active property of GameObjects. The replacement function, SetActive(), affects the child GameObjects as well.

Is there no replacement for toggling a single GameObject?

Comment
Add comment · Show 9
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 hvilela · Sep 27, 2012 at 09:47 PM 0
Share

I'm not using Unity 4 yet, but are you sure that SetActive affects the children? To affect them shouldn't be used "SetActiveRecursively"?

avatar image Cyberdogs7 · Sep 27, 2012 at 10:26 PM 0
Share

From the release notes : We have changed how the active state of GameObjects is handled. GameObjects active state will now affect child GameObjects, so setting a GameObject to inactive will now turn the entire sub-hierarchy inactive. This may change the behavior of your projects. GameObject.active and GameObject.SetActiveRecursively() have been deprecated. Ins$$anonymous$$d, you should now use the GameObject.activeSelf and GameObject.activeInHierarchy getters and the GameObject.SetActive() method.

Currently, .Active and SetActiveRecursively still work, but I am trying to prepare for when they don't

avatar image hvilela · Sep 27, 2012 at 10:32 PM 0
Share

Sounds like the GameObject.activeSelf is the new active and GameObject.activeInHierarchy is the new GameObject.SetActiveRecursively, right?

avatar image Cyberdogs7 · Sep 27, 2012 at 10:47 PM 0
Share

Both of those are ReadOnly members. SetActive is the only replacement I could find. It has the affect of triggering all children as well.

avatar image hvilela · Sep 27, 2012 at 10:52 PM 0
Share

Sorry man, I'm out out alternatives. $$anonymous$$aybe it's the new behavior, with no workarounds. When I think about it it makes sense to me, I think i never wanted to deactivate an object and keep his children running.

Show more comments

2 Replies

· Add your reply
  • Sort: 
avatar image
2

Answer by mcroswell · Nov 09, 2012 at 03:49 AM

It's really a paradigm shift, based on the fact that if you have a parent GO then if you turn it on, then the children that are on should also come on. If the children are off then they will stay off unless you explicitly change the children's state.

So, if the parent's off then the children are active. But, if the parent is active, the children may or may not be off.

Example code (requires a test GO and two parented children GO trees) is probably the best way to see this.

     public GameObject testGO;
     public GameObject GO_1;
     public GameObject GO_1_1;
     public GameObject GO_1_2;
     
     void Start () {
         PrintStates();
     } 
     
     void PrintStates()
     {
         PrintActiveSelf();
         PrintActiveInHierarchy();
     }
     
     void PrintActiveSelf()
     {
         print ("ActiveSelf for testGO=" + testGO.activeSelf 
             + " GO_1=" + GO_1.activeSelf 
             + " GO_1_1=" + GO_1_1.activeSelf
             + " GO_2_1=" + GO_1_2.activeSelf );
     }
     
     void PrintActiveInHierarchy()
     {
         print ("ActiveInHierarchy for testGO=" + testGO.activeInHierarchy
             + " GO_1=" + GO_1.activeInHierarchy 
             + " GO_1_1=" + GO_1_1.activeInHierarchy
             + " GO_2_1=" + GO_1_2.activeInHierarchy );
     }
     
     void OnGUI () {
         if (GUI.Button(new Rect(Screen.width/2, Screen.height/2-50, 200, 40), "Toggle just GO_1"))
         {
             GO_1.SetActive(!GO_1.activeSelf);
             PrintStates();
         }
         if (GUI.Button(new Rect(Screen.width/2, Screen.height/2, 200, 40), "Toggle just GO_1_1"))
         {
             GO_1_1.SetActive(!GO_1_1.activeSelf);
             PrintStates();
         }
         if (GUI.Button(new Rect(Screen.width/2, Screen.height/2+50, 200, 40), "Toggle just GO_1_2"))
         {
             GO_1_2.SetActive(!GO_1_2.activeSelf);
             PrintStates();
         }
 
     }
 }

I would have attached unitypackage but Answers apparently does not allow uploading of that data type attachment. So, here's a screen shot of the Hierarchy and Properties:

alt text

So, once you've set up the script, run it, press the buttons, look at the console. You'll notice that you can have a child that can have activeInHierarchy false, while still having activeSelf true. This is the key to understanding this: The child's activeSelf will not be changed by the parent, but the activeInHierarchy will.

Now, if you have to recursively set your children components you can write your own code to do that. I wish Unity would have left SetActiveRecursively() in, but I'm pretty sure I don't need it based on the new paradigm. What do you all think?


setactivetest_properties.png (58.6 kB)
Comment
Add comment · Show 2 · 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 paulsz · Nov 09, 2012 at 09:45 AM 1
Share

Thank you $$anonymous$$ichael, after long hours of frustration, I figured all of this out myself. It's actually pretty cool and very intuitive after you get rid of the old paradigm. It's very nice that in the editor you are not asked anymore to activate all children or just the parent. I hope this will also boost performance.

avatar image mcroswell · Nov 09, 2012 at 05:22 PM 0
Share

I agree Paul, it does seem intuitive now, and really is very cool. By the way, it was this original Question and your Answer and earlier insight (Oct 8) that made me realize it was a bit deeper situation than I first realized. I originally assumed it was just a recursive replacement, and came to Answers to find out I underestimated the situation.

I would guess, that it does improve performance too, because internally the engine can short-circuit its tree iteration when sending event messages, rendering and such to the children if the parent is off. Then when it is toggled, the engine doesn't even have to do the work of toggling the children's flags.

avatar image
0

Answer by paulsz · Oct 08, 2012 at 02:44 PM

So basically, there is no workaround to keep the same structure and code from the pre Unity 4. We just have to change and adapt the structure of the game objects in the scene and the code to adapt to the new game activation strategy. Now I can start modifying without worrying that there is an easier way :)

Comment
Add comment · Show 8 · 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 Cyberdogs7 · Oct 08, 2012 at 05:41 PM 0
Share

I was hoping someone at Unity would confirm. Can't say I am excited about this change...

avatar image paulsz · Oct 09, 2012 at 08:31 AM 0
Share

Same here, I have to change alot now and I'm not even sure how can I replicate the old behaviour :((

avatar image codedellsaber · Oct 16, 2012 at 11:34 PM 0
Share

Is the GameObhect.SetActive() soppose to leave all the gameobject scomponent's active??

avatar image ChrisYummi · Nov 26, 2012 at 04:48 PM 0
Share

Is a real shame that Unity won't comment. This problem will mean that many developers who would want to do a Windows 8 phone version (when it arrives) will really struggle to convert over. I'm sure $$anonymous$$icrosoft are not too pleased with this too. I agree it is better this way, but you can't just change such a key system in such a mature product.

avatar image Eric5h5 · Nov 26, 2012 at 05:36 PM 1
Share

Why would $$anonymous$$icrosoft care at all? And yes, if there's a good enough reason, then you can change any system, "key" or not. It's better to fix behavior that needs to be fixed rather than leave a sub-optimal solution in place just because "it's always been that way".

Show more comments

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

19 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

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

How to import the object from server to unity 2 Answers

Setting Scroll View Width GUILayout 1 Answer

Can someone help me fix my Javascript for Flickering Light? 6 Answers

Material doesn't have a color property '_Color' 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