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
2
Question by wightwhale · Aug 25, 2016 at 12:18 AM · performanceoptimization

Is there a performance gain by checking if an object is active

if (remObjs[i].activeInHierarchy) { remObjs[i].SetActive(false); }

Similar to this code. I'm trying to figure out if unity already has a check to see if the gameobject is active before I set it active again and maybe cause a bunch of extra code to be run.

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 DiegoSLTS · Aug 25, 2016 at 01:33 AM 0
Share

Activating an object calls OnEnable on the $$anonymous$$onoBehaviours attached, so I guess you can test this pretty easy. Try activating an already active gameObject and see if OnEnable is called again, or deactivating an already inactive gameObject and see if OnDisable is called twice.

I'm pretty sure it doesn't, but haven't tested that myself. I'm assu$$anonymous$$g Unity already checks if the state changes.

Also, note that "activeInHierarchy" and SetActive(true/flase) mean different things. activeInHierarchy can be false even if the object is active (if any parent is inactive). Also, SetActive(true) on an object that's not activeInHierarchy because of a parent being inactive won't make it active in hierachy (but will activate itself) and OnEnable will probably not be called. $$anonymous$$aybe you want to use isActive ins$$anonymous$$d of activeInHierarchy.

avatar image Ali-hatem · Aug 25, 2016 at 10:13 AM 0
Share
 if (!remObjs[i].activeInHierarchy){
  remObjs[i].SetActive(true); 
 }
avatar image wightwhale · Aug 25, 2016 at 09:38 PM 0
Share

I think I was a little unclear on this, say for example the object is active in hierarchy and I call SetActive(false) then the game object goes inactive no problem.

But if the object isn't activeInHierarchy and it's not activeself then does calling SetActive(false) incur a performance hit?

Is it better to make sure it's already active before setting it inactive, or is it better to set it inactive even if it's already inactive. That's the performance I'm concerned with.

Also Unity has deprecated isActive so we can't use that going forward.

1 Reply

· Add your reply
  • Sort: 
avatar image
2

Answer by Bunny83 · Aug 25, 2016 at 10:14 PM

No, there's nothing to worry about if you deactivated an deactivated object again or if you activate an already activaed object.

Im most cases however it's to consider if you can simply avoid a frequent updating of objects. You should rely more on events instead on polling a certain state if possible.

As a side note: activating a deactivated object or deactivating an active object can have quite a performance hit. However it depends on what components it has attached. In general activating / deactivating physics objects (objects with rigidbody / collider) is quite heavy. If you "pool" physics objects you might consider to just disable the renderer and move it far out where it doesn't hit anything until you need them again.

Also keep in mind when checking "activeInHierarchy" before activating an object doesn't really make much sense. The current "active setting" of an object should be read with activeSelf. activeSelf directly corresponds to the active state of the object itself. So if activeSelf is false and you call SetActive(true) it is guaranteed that activeSelf is now true. This might not hold true for activeInHierarchy if a parent is also deactivated. So a child can be activated but is still deactivated due to the "active state inheritance".

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

74 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

Related Questions

Performance gain through recycling Objects? 1 Answer

What's a good draw call count? 2 Answers

Android Best performance 1 Answer

Need optimizing tips for mobile third person shooting game. 2 Answers

Frame Drop on Touching or Swiping Screen 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