Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to detect if there is an error in unity c# when running without see the console? [duplicate]

Tags:

c#

unity3d

Is it possible to detect an error in a C# script running in Unity without reading the console log ? I need this when I have to build the game and test it in a mobile. If there is an error when running it will display a message box that shows the error.

I understand that we can use Unity Log Viewer to print all the Log in the device. But I ask for another way to do this. I ask a simpler solution. My solution I think it is a best to detect a minor errors when it runs successfully in editor, but has problems when running in a device due it just displays a showMessageBox Error.

I need to detect if something is wrong when running. I realize that there is Debug.LogError which we can detect an error. But Debug.LogError just print the message type by us to pointing what the object error is. What I need is to detect a global error like console and show error message from unity engine.

What I need may be something like :

void Update() {
       showMessageBox(isErrorDetect());
}

showMessageBox => is a function to show message box.
isErrorDetect => this will print an error if detect like a console.

If someone understand what I mean, then please, give me a solution.

Thank You

like image 512
Dennis Liu Avatar asked Jan 03 '23 16:01

Dennis Liu


1 Answers

So no one understand what i mean. But i got the solution my self.

We can use : Application.logMessageReceived that what i mean.

Script below will popup if there is an error when running the game at device. Not tested yet. But iam sure it work. It will help you when no error running in editor but got error when running at mobile.

I make it myself. Example :

using UnityEngine;
using System.Collections;
using UnityEngine.UI;

public class ErrorHandlePopUp : MonoBehaviour {

    public Image PopUp;
    string error;

    void OnEnable() {
        Application.logMessageReceived += HandleLog;
    }

    void OnDisable() {
        Application.logMessageReceived -= HandleLog;
    }

    void HandleLog(string logString, string stackTrace, LogType type) {
        
        if (type == LogType.Error) {
            error = error + "\n" + logString;
            PopUp.gameObject.SetActive (true);
            PopUp.transform.GetChild (0).GetComponent<Text> ().text = "Error";
            PopUp.transform.GetChild (1).GetComponent<Text> ().text = error;
        }   
    }

    public void Dismiss() {
        PopUp.gameObject.SetActive (false);
    }

}
like image 95
Dennis Liu Avatar answered Jan 06 '23 06:01

Dennis Liu