While asking questions, and reading answers in other posts one thing that I have noticed is that people often utilize the debug.log as part of their solutions.
I cannot however for the life of me, figure out why I would ever want to use it, and in general I tend to skip the debug part of peoples suggestions.
I’ve tried to search on the topic, but cannot really find much, so could one of you explain to me why the usage of debug.log is important and when it should be used?
Debug.Log Can be used for many different scenarios/situations whether it be ensuring functions are being called when they’re supposed to be, checking if anything is running more than once if not intended…
Basically its main use for myself is to identify the root of problems that could potentially be encountered in code.
Use it whenever you feel its necessary, (Checking lists are initialized, Checking functions are running, are functions returning the correct variable?) etc…
It’s a simple test method to see if the program is doing what you expect it to do. It’s especially useful when you get an error or when you are first writing a program to make sure it’s working up to that point. For instance, say you are going to use some type of interactivity eventually. At first you can do a debug.log in a function to say what happened when the player got near a trigger. In debugging, you might have gotten an error that isn’t clear where it was pointing to, so use a debug.log to output some information and see if that part of the script is working.
“I’ve tried to search on [why Debug.Log is used], but cannot really find much,”
You’re looking for a introductory book on computer programming. A typical one covers up through functions and arrays, at least, and explains why we think and organize in the way we do.
It is good for when troubleshooting an issue you don’t understand. Just pepper in Debug.Log messages everywhere and you’ll be able to see what is actually happening instead of assuming what you expect is happening. Say you expect one method to call another, go into a while loop, which eventually calls another, and then accesses a method on another component. You hit play, and you don’t see the result of the final method call. Why isn’t it working??!! This can be frustrating, but just put Debug.Log at every point, output any values you are using, hit play, and you immediately see where your logic is breaking down.
It is also useful when getting into areas of your code you don’t expect. For example when I have a switch statement that should never go to Default, I also add Default and throw a Debug.Log in there that says in what script this happened.
Be careful to not put Debug.Log messages in as a regular part of your update loop though. I spent an hour once trying to figure out a significant performance drop in a game I was making a year ago, only to be surprised it was caused by 3 or so Debug.Log calls I was making per update loop in an area of code I was recently debugging but didn’t think it important at the time to comment out when I was done.
Like Joe-Censored said, use Debug.Log() when you are troubleshooting. I would never keep any Debug.Log() code around when i’m not needing it, as it is very slow and will also produce spam in your debug console.
I totally recommend to make use of Assertions where something can go wrong. Those are only showing in the debug console if the assertion fails and they will not influence the performance of your final game at all (they are quite literally removed from the code).
It’s good practice to use assertions to make sure that the parameters passed to a function are in the correct range.
Assume a function that takes a GameObject:
Now you have made sure that the GameObject can never be null, otherwise the assert will write an error to the console.
Just make sure that the assert does not contain any code that alters the state of a variable, stick to comparing variables.
I mean, if you never make any mistakes and always write your code perfectly the first time and are certain that Unity is also always doing everything correctly and so you never have to debug, then sure, you’d never need to use Debug.Log. Of course if that’s the case, though, then you’re probably a Cylon and have bigger problems to worry about.