Throw exception with a contextual UnityEngine.Object

Viewed 358

In Unity, we can use Debug.LogError (and other log methods) to define a context parameter along with the message. For example:

Debug.LogError("I am an error", aContext);

The code above will print an error to the Unity Console and then we can click the error to highlight the object passed as context in the Hierarchy window. (By "object" I always mean UnityEngine.Object and derivatives). The method Debug.LogError does not raise an exception nor it stop the execution of the method, it prints the error silently.

Also in Unity, we can throw a System.Exception like any other C# program. Even it is not a "Unity thing" (it is from C#), it gets caught in the Unity console. We can do that simply by calling throw new System.Exception("My message here").

Question: There is some way to throw some type of Exception with a context similar to the Debug.LogError, so when I click the console it highlights an UnityEngine.Object passed as parameter? I do not want to call both Debug.LogError and throw because it would be duplicated in the Unity console (this is the only thing I complain about).

What I expect is to call throw new SomeSpecialOrMadeException("My message here", aContext);, so it behaves like a C# exception, but it highlights the object in the Hierarchy window when I click the error in the console at same time.

0 Answers
Related