Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question UnityException in ToString: GetName is not allowed to be called during serialization

Discussion in 'Scripting' started by termway, Sep 30, 2023.

  1. termway

    termway

    Joined:
    Jul 5, 2012
    Posts:
    37
    Hello there,

    I want to get the name of the object during serialization for debugging purposes but it throws me this error message.

    UnityException in ToString: GetName is not allowed to be called during serialization, call it from Awake or Start instead. Called from MonoBehaviour 'XXX' on game object 'YYY'.


    Is there a good and simple workaround for this?

    The funny thing is that the information is known by Unity because it gives me the information I want in the exception message.
    So I guess I could parse the exception message to get the information but that seems ugly and overkill to me Or, I could simply keep the exception message because it does contain the information I want after all.
     
  2. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    3,721
    Post your code.

    Rather than ToString() does maybe the name property suffice? Plus maybe the type of the object.
     
  3. termway

    termway

    Joined:
    Jul 5, 2012
    Posts:
    37
    Well I use the ToString() method because it gives me the name of the object in the exception message. If i use the .name property, I have the same error.
    Maybe the title on my post is not adequate here what I want is to get the name of the game object in that method.
    The code is executed in edit mode by the way and there will be multiple objects.

    Code (CSharp):
    1. public abstract class XXX: MonoBehaviour, ISerializationCallbackReceiver
    2. {
    3.     void ISerializationCallbackReceiver.OnAfterDeserialize()
    4.     {
    5.         //...
    6.         Debug.Log($"[{GetType().Name}][{GetInstanceID()}] {nameof(OnAfterDeserialize)} {this}.");
    7.     }
    8. }
     
  4. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    3,721
    I guess you‘ll have to make do without the object name then. I would understand that during serialization the this object is pretty much off-limits since any method call could alter the state of the object, possibly breaking serialization. The exception seems very reasonable because it prevents hard to track down bugs.

    Maybe if you explained why you need to know this and what you are doing within the callback an idea for a workaround or alternative may come up.
     
  5. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    3,721
    Here‘s one: add a [NonSerialized] (or private) string field myObjectName and assign it gameObject.name in Awake. Try logging that field in the serialization callback.
     
  6. SisusCo

    SisusCo

    Joined:
    Jan 29, 2019
    Posts:
    1,097
    If you just need to do this in the editor, you can easily postpone the logging to take place on the main thread after deserialization has finished using EditorApplication.delayCall.
    Code (CSharp):
    1. EditorApplication.delayCall += ()=> Debug.Log(ToString());