- Home /
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 :)
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.
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 ?
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).