I want to create a log file for every time my WebGL game is played and have the errors and warnings with detail in there. I know how to make the log file I just don't know how to get the errors and warnings and their details, and I want to have the line number too.

The only way I know of getting an error is by Try Catch but that is not working in Unity or at least in WebGL build. The manual states that the log is output to the browser's JavaScript console, but I don't have access to all of my play testers and sometimes I can't reproduce the bugs they report.

  • 1
    \$\begingroup\$ If you don't want to get downvoted I would recommend to post your current code :) \$\endgroup\$
    – TobiasW
    Feb 1 '17 at 9:09
  • \$\begingroup\$ I don't have any. \$\endgroup\$
    – Henjin
    Feb 1 '17 at 9:14
  • 1
    \$\begingroup\$ Have you checked Unity's manual for log files? docs.unity3d.com/Manual/LogFiles.html \$\endgroup\$
    – n_plum
    Feb 1 '17 at 13:07
  • \$\begingroup\$ "WebGL On WebGL, log output is written to the browser’s JavaScript console." This is all it says, @n_palum is what I'm trying to do, impossible? \$\endgroup\$
    – Henjin
    Feb 2 '17 at 4:57
  • 1
    \$\begingroup\$ Perhaps this question is just simply too broad? It is also difficult to tell if your trying to write this as a compiler. Some of the actions you reference (eg. knowing the specific line number) sound more like compiler functions. \$\endgroup\$
    – Gnemlock
    Feb 2 '17 at 5:14

As a WebGL game, you're running it in a web page.

You can edit the HTML to add an extra bit of JavaScript to intercept the browser's console log before your Unity game loads and tries to use it, as detailed in these StackOverflow answers by Denys Séguret and OneOfOne.

var store = [];
var oldf = console.log;
console.log = function(){
   oldf.apply(console, arguments);

This will let you catch everything Unity would output, even if it's not coming from one of your own scripts that you can wrap in a try...catch. Don't forget to enable exception support as described in this article from the Unity docs, as it's limited by default in WebGL builds.

Once the game ends (or encounters a problem) you can have your script bundle up the contents of its store array into a log file to save or transmit. You can also do this periodically in case of an unexpected shutdown, and so you don't add delays when the player is trying to navigate away or close the tab.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.