Unity Tips 02 – View private properties on Unity inspector

Today, i will share with you the way to display private variables / properties into the Unity 3D Inspector. It’s useful when need to know the value taken by a private property whitout writing code like “Debug.Log”

whitout debug mode activated, you cannot see private members
with “debug mode” activated, you can

As you can see, in the second screenshot, we are seeing private members from the NetworkBehavior where the ItemMapSpawner is descending

Feel free to ask your questions here

Unity Tips 01 – Duplicate component on the fly

One day or another, you will be in front of duplicating an Unity 3D gameobject component on the fly. You think that it’s easy to do and you think that Unity provides you a simple way for doing that like a this:

gameobject.Add(otherGameObject.GetComponent());

Unfortunately, it doesn’t work. The trick is to create an extension for the GameObject class. Here is the implementation :

using UnityEngine;
using System;
using System.Reflection;
using System.Linq;

/*
*   Gameobject Class Extension
*/
namespace Utilities.Extension
{
    public static class GameObjectExtension
    {
        /// 
        /// Check it the gameobject has the specified component
        /// 
        public static bool HasComponent(this GameObject go) where T : Component
        {
            return go.GetComponentsInChildren().FirstOrDefault() != null;
        }

        /// 
        /// Return a copy of the component
        /// 
        public static T GetCopyOf(this Component comp, T other) where T : Component
        {
            Type type = comp.GetType();

            if (type != other.GetType()) return null; // type mis-match
            BindingFlags flags = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Default | BindingFlags.DeclaredOnly;
            PropertyInfo[] pinfos = type.GetProperties(flags);
            foreach (var pinfo in pinfos)
            {
                if (pinfo.CanWrite)
                {
                    try
                    {
                        pinfo.SetValue(comp, pinfo.GetValue(other, null), null);
                    }
                    catch { } // In case of NotImplementedException being thrown. For some reason specifying that exception didn't seem to catch it, so I didn't catch anything specific.
                }
            }
            FieldInfo[] finfos = type.GetFields(flags);
            foreach (var finfo in finfos)
            {
                finfo.SetValue(comp, finfo.GetValue(other));
            }
            return comp as T;
        }

        /// 
        /// Copy the component
        /// 
        public static T AddComponent(this GameObject go, T toAdd) where T : Component
        {
            return go.AddComponent().GetCopyOf(toAdd) as T;
        }
    }
}

For more information about class extensions :
https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/extension-methods