Skip to content

Instantly share code, notes, and snippets.

@joonjoonjoon
Last active August 29, 2015 14:06
Show Gist options
  • Select an option

  • Save joonjoonjoon/2f9b73c5b20010fb42f2 to your computer and use it in GitHub Desktop.

Select an option

Save joonjoonjoon/2f9b73c5b20010fb42f2 to your computer and use it in GitHub Desktop.
using System;
using Holoville.HOTween.Path;
using UnityEditor;
using UnityEngine;
[CustomEditor(typeof (SomeMonobehavior))]
public class SomeMonobehaviorEditor : Editor
{
public override void OnInspectorGUI()
{
DrawDefaultInspector();
if (GUILayout.Button("remove and add component"))
{
var gameObject = ((SomeMonobehavior) target).gameObject;
try
{
// Illustrating with BoxCollider as example
var old = gameObject.GetComponent<BoxCollider>();
if (old != null)
{
DestroyImmediate(old);
}
}
catch (NullReferenceException)
{
// this occurs properly when the object does not exist (though the != null is catching it now)
}
catch (MissingReferenceException)
{
// the code never enters here, even though the exception is of this type.
}
// add it again
var path = gameObject.AddComponent<BoxCollider>();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment