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
0
Question by Inan-Evin · Feb 02, 2015 at 06:59 PM · performancebooleandisableenable

Performance Question, Change Boolean or Enable-Disable Script

Hello guys, I am writing a script for detecting interactable objects around the scene, like light switches, lamb switches, doors, wardrobe handles and things like that. My system is like this : I have an empty game object with raycast script attached, and it is a child of my player. Each interactable object has a script on it and that script determines what to do when interacted. My raycast script is nothing but a simple raycasting and checking tag. Now, in each level I have a level manager that controls things on the scene. To save from performance, I don't want the player to cast any rays if there are no interactable objects around. Detecting if there are some interactable objects around is handled by my level manager scripts. My question is, what should my level manager script do when there is no detectable object around ? Disable the game object that has raycast script on, disable the raycast script, or just change a public boolean in raycast script to false in order to prevent it's update from casting a ray everyframe. I know the result will not differ a lot in a scene like this, because I am just casting one ray and it is nothing. But this question is actually something that I want to know, what is the best way to handle a situation like this, what if I cast 1000 rays in that raycast script and want to disable - enable , I really would like to know the performance issues amongst things like these. You know for the first option, I have to define a transform in level manager to reach it's gameObject and disable it, which will hold a place in memory, for the second option I have to define the Script and drag&drop in inspector, still this will hold place in the memory, on the third option, if I make that boolean static I don't have to define anything but still I have to use ScriptName.booleanName, and I don't know if it is something optimized or not, you see my point, I have a lack of knowledge about these things, what is the best way and can please someone give me an overview explanation about these topics ? Thanks in advance :)

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 RudyTheDev · Feb 02, 2015 at 08:13 PM 2
Share

What you are discussing in your real case is micro-optimization. Unless you are actually having issues, it will not be worth "solving them".

What you are discussing in your later examples is algorithm optimization. You would not solve them the same way as low-level micro optimizations.

PhysX is already smart enough to group static colliders and make raycasting as quick as it can in a general-purpose environment.

The only sure way to find out performance is to benchmark it yourself. Almost always high-level problems like the hypothetical "1000 rays" can be solved by algorithm design. For example, you would just raycast less times or less frequently. You wouldn't go switching scripts on and off for that.

For example, you can disable your raycasting when nothing is around. But how do you know nothing is around? Do you have another script running each frame checking distances from player to all objects? That will be an order of magnitude slower than just raycasting. It doesn't matter if you enable them, keep a static bool, or toggle them otherwise. The very act of trying to toggle them is costing more than the "problem" you were trying solve.

avatar image Inan-Evin · Feb 02, 2015 at 09:01 PM 0
Share

Well actually my scenes are designed in a way that each entrance has triggers on it, and when player touches the enter trigger, it will enable it to check interactable objects around, and when player touches the exit trigger, it will disable the checking, so my "checking if interactable objects around" script is not very costy, only thing I was curious about was that 3 ways, and now I got what you mean. Just to clarify, so you say it will not differ a lot that we can tell, only way to see that small difference is trying them seperately and checking results, did I get it right ?

avatar image RudyTheDev · Feb 03, 2015 at 12:16 PM 1
Share

I guess what I'm saying is that even if your methods have 10x speed difference, that is still times and times faster than whatever else is happening and doesn't really matter. And if it starts to matter once you have thousands of them, then you probably shouldn't be doing it that way.

I can't tell which of your methods may be slower, because their description isn't really clear (hence I didn't post this as an answer).

0 Replies

· Add your reply
  • Sort: 

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

Same script working on one object at a time. 1 Answer

using Aim Constraint 1 Answer

Activate one canvas and deactivating the other. 1 Answer

How resources-consuming are disabled things? How much drain puts enable/disable components/GOs on the system? 2 Answers

Is there a hidden cost of enabling/disabling something that is already in that state? 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