- Home /
Problem is not reproducible or outdated
C# Extension method not found
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? $$anonymous$$y Transform extender looks pretty much like yours and it works fine.
Yeah and you're not using it from a folder under plugins - or some kind of Standard Assets or something?
Right, 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?
Answer by huntsfromshadow · Feb 28, 2014 at 05:13 PM
It's the namespace. Get rid of the namespace and it should work.
Also your second method doesn't have a 'this' argument in it's arguments.
$$anonymous$$y original code didn't have the namespace, and it didn't work:
using UnityEngine;
using System.Collections;
public static class TransformExtender
{
public static Transform FindRecursive
(this Transform t, string name)
{
return FindRecursive(name, t);
}
private static Transform FindRecursive
(string name, Transform current)
{
//Implementation
return null;
}
}
I added the namespace to try to explicitly include it in the usage file
Is the name of the file of the TransformExtender the same as the name of the class? Is it TransformExtender.cs ?
Only other thing I can think is maybe the Variable T is throwing the compiler in your monobehavior script as T most time is used by the generic system.
It is not a namespace problem. If it was, the second usage won't work either.
Also, the second method is not intended to be an extension method that's why it lacks the thisin the first parameter
Yea I admit I'm stumped. I pulled your code into an empty project in 4.3.4 and it compiles fine.
Follow this Question
Related Questions
Distribute terrain in zones 3 Answers
Multiple Cars not working 1 Answer
Unity's mouse look causing movement issues 1 Answer
Smooth out character movement? 4 Answers
stop object(door) translation 0 Answers