Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
1 capture
11 Jun 22 - 11 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 RLin · Aug 03, 2015 at 09:18 PM · gameobjectdisablesetactiveenable

Calling setActive(false) on a disabled object?

I have read that calling setActive is performance heavy, and should be avoided when possible. I can confirm this, as the game hiccups when calling setActive on large numbers of objects. If I call setActive(true) on an already enabled object, or setActive(false) on an already disabled object, will this still cause lag despite the object state not changing?

Comment
Add comment · Show 1
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 Dave-Carlile · Aug 03, 2015 at 09:21 PM 0
Share

Run it through the profiler and try it out.

3 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by TimHeijden · Aug 04, 2015 at 01:18 AM

Like Dave said, try it out :P

It doesn't sound likely though, the reason SetActive() would be performance heavy is because it triggers all kinds of things. An example is that all components (for scripts, OnEnable/OnDisable) on the gameObject AND all child gameObjects have to be activated. Colliders / rigidbodies probably have to be (un-)registered to the physics engine and so on.

When an object is already enabled or disabled, one would assume Unity would be competent enough to detect this and not cause performance issues.

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 TonicMind · Dec 17, 2015 at 04:36 PM

I have to correct TimHeijden said. As with anything SetActive() is not inherently (by default) inefficient. It depends on the situation! If you are constantly reusing the same prefab then it makes more sense to use an object pool. Doing that saves a lot of processing power. I recently implemented an object pool in my game and it gets a full 60 fps now. Think about it, if you need something many times per second why would you just create a new one every time it was needed? That was actually more work than when I implemented the object pool!

Why go through all that work when you can call one method to disable it?

Check this out: https://unity3d.com/learn/tutorials/modules/beginner/live-training-archive/object-pooling

They use an objectpool which does use SetActive() many times a second for their repetitive task. It doesn't cause efficiency problems at all!

I just wanted to update this question I found while searching for information on something, because I think the selected answer is misleading.

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 Zahidylin_Marat · Aug 08, 2018 at 08:21 AM

Was thinking many times:

So if object is already not active gameObject.activeSelf == false than

 gameObject.SetActive(false)

have the same performance as

 if(  gameObject.activeSelf == false) gameObject.SetActive(false)

If call it in Update all the time:

 void Update()
 {
    gameObject.SetActive(false);
 }

Right?

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

27 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

Related Questions

Alternatives to GameObject.SetActive(...) 2 Answers

Bool activate/de-activate on timer C# 1 Answer

I Can't disable gameObject using .SetActive (or anything else) 2 Answers

enabling / disabling child objects 1 Answer

Scipt Not Functioning Properly 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