I’ve written an extension method for UnityEngine.Transform but Unity doesn’t like it. If I explicitly call the extension method, it works fine, but otherwise it doesn’t.
Extension:
using UnityEngine;
using System.Collections;
namespace CustomExtensions
{
public static class TransformExtender
{
public static UnityEngine.Transform FindRecursive
(this UnityEngine.Transform t, string name)
{
return FindRecursive(name, t);
}
private static UnityEngine.Transform FindRecursive
(string name, UnityEngine.Transform current)
{
//... Implementation of recursive search not relevant to the question...will post in comments if anyone cares.
return null;
}
}
}
Usage:
using CustomExtensions;
...
void Start(){
Transform T = transform.FindRecursive("somegrandchild");
Transform R = TransformExtender.FindRecursive(transform, "somegrandchild");
}
The first usage gives me the error:
error CS1061: Type UnityEngine.Transform does not contain a definition for FindRecursive and no extension method FindRecursive of type UnityEngine.Transform could be found(are you missing a using directive or an assembly reference?)
The second usage works fine. For what it’s worth, Visual Studio intellisense understands the extension. If I type transform.F it gives me the three options “Find, FindChild, FindRecursive(extension)” but for some reason the unity compiler doesn’t like it.
Any ideas what I’m doing wrong?
UPDATE:
So, I was playing with it some more and it turns out that it does indeed work some of the time. In particular
Transform upperArm = transform.FindRecursive(“upper_arm_R”);
string joint = “upper_arm_R”;
Transform upperArmB = transform.FindRecurisve(joint);
the first instance works, the second does not( throws the CS1061 error). I’m not sure why those would be different, but I believe the Extension is probably not the error…
“Extension method that takes a string works fine with string literal, but fails with variable of type ‘string’”
Any thoughts?
Something to do with putting the static class in a normal folder and trying to use it from a Plugins folder? My Transform extender looks pretty much like yours and it works fine.
– whydoidoitI just have it in a "Scripts" folder. Perhaps I'll just try rewriting it from scratch (the old, turn it off and back on again approach)
– perchikYeah and you're not using it from a folder under plugins - or some kind of Standard Assets or something?
– whydoidoitNope, right now everything is in the Scripts folder(in the toplevel of the project)
– perchikRight, well that's weird then. Be interested to hear if you can make it work with the rewrite... aAre you letting Unity build it or building it externally?
– whydoidoit