Unable to load script, when referencing an extension method

I have a really simple extension method that I am calling from a MonoBehavior. When I use the method the Unity project fails to compile and states that the Behavior that is using the extension method can not be loaded, please fix compile errors and assign a valid script.

I’ve included the commented code in the behavior that works if I comment out the extension method call.

There are no errors in the Visual Studio project, and everything appears to be in order.

The extension class:

public static class TransformExtensions
{
    public static void Clear(this Transform transform)
    {
        foreach (Transform child in transform)
        {
            Clear(child.transform);
            GameObject.Destroy(child.gameObject);
        }

        GameObject.Destroy(transform.gameObject);
    }
}

The behavior

public partial class MeasureManager : Singleton<MeasureManager>
{
        void DeleteMeasurement(Measurement measurement)
        {
            //while (measurement.Container.transform.root.childCount > 0)
            //{
            //    DestroyImmediate(measurement.Container.transform.root.GetChild(0).gameObject);
            //}
            //DestroyImmediate(measurement.Container.transform.gameObject);

            measurement.Container.transform.Clear();

            Measurements.Remove(Measurements.Find(x => x.Name == measurement.Name));
            _current = null;
        }
}

I’m going to hazard a guess that Clear(child.transform) should be child.transform.Clear(). With the code you’ve provided, it shouldn’t even compile in Visual Studio…

the method Clear(Transform transform) is not defined (which is completely separate from Clear(this Transform transform))

also if Unity says that its failing to load a Behaviour script that usually a sign that the filename doesn’t match the class name. Also only one Behaviour should be in a file. If you have multiple Behaviours in the same file (even if at least one matches the filename) Unity will fail to compile due to the other Behaviours not having their own file

In the end, this was a namespacing issue. I was piggy backing off some of the HoloLens Toolkit code and was using their namespace. For some reason this caused problems when trying to access my extension methods that we’re not in the same or no namespace.

I don’t understand why Unity took issue, when Visual Studio resolved everything correctly.