Skip to content

Logging an entire GameObject

by AngryAnt on September 17th, 2009

More stuff from the shadowy corners of my hard-drive. Don’t remember the context, but someone needed to log every single piece of information available on a particular GameObject. I suppose this could be useful for end-user “This GameObject Just Went Completely FUBAR” ™ scenarios.

Anyway – it has reflection in it which by definition makes it cool.

Le codez:

using UnityEngine;
using System.Reflection;

public class Utilities
{
    /* ... */

    static void LogGameObject( GameObject gameObject, bool children )
    {
        Component[] components = gameObject.GetComponents( typeof( Component ) );
        FieldInfo[] fields;
        PropertyInfo[] properties;

        Debug.Log( gameObject.name + ":" );

        foreach( Component component in components )
        {
            Debug.Log( " - " + component.GetType().Name + ":" );
            fields = component.GetType().GetFields();
            foreach( FieldInfo field in fields )
            {
                Debug.Log( " ." + field.Name + " = " + field.GetValue( component ) );
            }

            properties = component.GetType().GetProperties();
            foreach( PropertyInfo property in properties )
            {
                Debug.Log( " ." + property.Name + " = " + property.GetGetMethod().Invoke( component, null ) );
            }
        }

        if( children )
        {
            foreach( Transform transform in gameObject.transform )
            {
                Debug.Log( "->" );
                LogGameObject( transform.gameObject, children );
            }
        }
    }

    static void LogGameObject( GameObject gameObject )
    {
        LogGameObject( gameObject, false );
    }

    /* ... */
}

From → Tips and tricks

2 Comments
  1. Reviewing my post I just realised that this will break if you have components on the GameObject which hold properties with no get defined.

    Then again if you have a setup like that, you’re one crazy bastard and I have no desire to support your unholy coding.

  2. Great stuff! Perhaps this cam help me diagnose why my sphere object is going completely “FUBAR” when user interaction scales it too rapidly. Hopefully I will be able to test it out soon.
    iSensorii: http://vimeo.com/vjanomolee/videos

Leave a Reply

Note: XHTML is allowed. Your email address will never be published.

Subscribe to this comment feed via RSS

*

Spam protection by WP Captcha-Free