I wonder if there's a way to turn off all debug.drawray/drawline calls without if checks?

Debug.logger.logEnabled=false;

I found that one, which is super great; now I can string Debug.Logs that are sort of silly/redundant/useless informing me of my game system’s states everywhere and also turn the debug off without turning it off in the build settings.

The reason that’s a concern is that Debug.Log does a stack trace (which is good) but that takes time, so it’s nice to be able to toggle it. Drawray doesn’t do a stack trace, naturally.

I wish there was a similar toggle for drawing rays and lines. I wonder if I could wrap it somehow?

I could put Debug.Drawray in my own static method that way I wouldn’t need to rewrite an if-check every time, that’s what I’m gonna do now, but it does an extra if-check for every one. Then again, then I have a method call to a method doing nothing even if I turn debugging off in the build settings, so maybe that’s a bad idea?

You guys typically just leave your draw rays on all the time? I don’t feel like making them and then removing them, I want to leave them in so I can see em’ in the future.

PS I wish this setting existed for 3D raycasts, too:

Use conditional compilation:

#if UNITY_EDITOR
// do stuff only in editor; code does not exist in builds
#endif

–Eric

2 Likes

Oh, thank you. If I turn off the debug in the build settings, wouldn’t that be doing the same thing though?

No, that’s only for Debug.Log; other stuff like DrawRay calls are still in the build. (So is Debug.Log for that matter, but I assume the Log function just returns without doing anything in that case, like DrawRay. However they are still technically taking time to run, just not very much.)

–Eric

Interesting, and good to know.

With that knowledge I’ll do both things. I’m gonna make a class that just draws rays and such with my if statements to toggle the debug work on and off, then I’ll put it and every call to it as conditionally not compiled.

It wouldn’t really be worth doing but I’m doing some stuff with Vector2’s and I’ve got a couple little methods that just draw a ray given Vector2’s instead of 3’s.

OK, I would like to toggle all Debug.Log statements on and off, so I searched and stumbled on this pretty recent thread. Any hints on where to change this: Debug.logger.logEnabled=false; would be super helpful!

Why not create a static bool in one of your classes called Global_Enable_DebugLines. Have it be false by default. In a class like a game manager class have this in the Awake function:
#if UNITY_EDITOR
GameManager.Global_Enable_DebugLines = true;
#endif

Remember to make the variable false in the OnDestroy() method

Then search your entire project and do a replace:
Original: Debug.DrawRay
Replace With: if (GameManager.Global_Enable_DebugLines) Debug.DrawRay

You can also have on-off switches for a particular script instance, so that only certain instances can do debug drawing if debug draws are enabled at all. Then your check becomes
Original: if (GameManager.Global_Enable_DebugLines) Debug.DrawRay
Replace With: if (GameManager.Global_Enable_DebugLines && this.debugDrawEnabled) Debug.DrawRay

Having a class to handle all debug draws is good program design but for a game I think it may be better to avoid the overhead of the extra method call. In your build you’d be wasting CPU time on method calls that do nothing. I’d personally prefer to just prevent the attempt of drawing the ray.

Also check out the conditional attribute.

You could put this on a single method that draws the ray. That way all your calls will be compiled out without needing #if all over the show.

Just wanted to say I was revisiting this topic because my solution was clunky and yours is a brilliant answer.