Using 'this' in static method parameter(extension methods) getting compiler error in Unity 5.5

Hi, I just updated to Unity 5.5.0f3 and am getting compiler errors for using extension methods. When I do:

public static string ColorText(this string inputString,string colorString)
{
    return "<color=" + colorString + ">" + inputString + "</color>";
}

I get a compiler error. This was never a problem in Unity 5.4. Is this not working properly in Unity 5.5?

This is the error I get from the compiler:
Type string' does not contain a member ColorText’ and the best extension method overload `GameCon.ColorText(this string, string)’ has some invalid arguments.

it means that the the parameter your passing as “colorString” is not a string and the compiler is complaining because its expecting a string. follow the stack trace and see what is calling the extension method with the wrong parameters

It is being passed in a string, here is an example of its use:

string colorString = "#FFFFFF";

"Text here".ColorText(colorString);

This was compiling correctly in Unity 5.4 but it’s no longer compiling in 5.5.

Just out of curiosity, why not just make it accept colours?

Remove ‘this’ ?

I feel like you need to learn about extension methods…

2 Likes

Possible :wink: @Dameon can you shed some light?

To me, it looks wrong in the OP (at least in the one posted code line, maybe there is more to it…).

Edit: just googled it, yes I did not know about this. Great, something new - sorry to OP

First hit had the following note, maybe useful for OP to check if that’s where an error might come from…(maybe point 1)

Important points for the use of extension methods:

  • An extension method must be defined in a top-level static class.
  • An extension method with the same name and signature as an instance method will not be called.
  • Extension methods cannot be used to override existing methods.
  • The concept of extension methods cannot be applied to fields, properties or events.
  • Overuse of extension methods is not a good style of programming.

Thanks for the replies everyone.

TaleOf4Gamers - What it does is allow me to easily add rich text color to a string. Using regular text color in unity affects the whole entire text object, not individual pieces inside of it. I should probably name the method a bit better to not allow for the confusion that you had. I have edited the top comment with the whole method as well just so people can understand it better:

public static string ColorText(this string inputString,string colorString)
{
    return "<color=" + colorString + ">" + inputString + "</color>";
}

I completely understand that I could do this whole thing another way to make it compiled but I use this utility quite a lot and have plenty more of these extension method utilities that I use. It makes my code look much cleaner and it would be a huge pain to change all of these to use a regular method instead. I’m concerned that this C# principle does not seem to work correctly in a new version of Unity. Especially one that has completely redone the compiler from what I understand.

Here is another example of an extension method I use which turns a color hex string into a Color32. This has stopped working as well:

public static Color32 ToColor(this string hex)
{
    byte r = byte.Parse(hex.Substring(0,2), System.Globalization.NumberStyles.HexNumber);
    byte g = byte.Parse(hex.Substring(2,2), System.Globalization.NumberStyles.HexNumber);
    byte b = byte.Parse(hex.Substring(4,2), System.Globalization.NumberStyles.HexNumber);

    return new Color32(r,g,b, 255);
}

public void ExampleUse()
{
   Color newColor = "#FE35A7".ToColor();
}

sngdan - All your points are being correctly implemented. This extension method exists in a top level static class. It’s not being duplicated/overridden. I’m only using it on methods. I can’t tell if I’m overusing it but for me it is incredibly useful and makes the code cleaner/easier to read.

1 Like

I would post in the 5.5 Beta forums. they have a thread for reporting bugs discovered in 5.5. of course the forum has been archived in place of the newer 5.6, its still a place you should look into.

it may even be something as simple as reloading the assembly or target framework

Thanks JoshuaMcKenzie I will post this in the version forums instead.

From the exception, it’s recognizing your extension method (“and the best extension method overload blahblahblah”), but it’s not recognizing the arguments.

It might be an error in the new compiler. Does it work if you invoke the extention method statically (ie. StringExtensions.ColorText(“Text here”, colorString))?

3 Likes

I tried it by doing:
System.Web.WebPages.StringExtensions.ColorText(“Text Here”,colorString);

and I get:
The type or namespace name Web' does not exist in the namespace System’. Are you missing `System.Web’ assembly reference?

Maybe I am not correctly trying to access StringExtensions but that’s what it said the inheritence was in the msdn docs:

not that “StringExtensions” class… your public static class which you’ve written ColorText in. Since you didn’t provide the name of the class where you defined the extension method, Baste simply assumed you named it StringExtensions because its the normal convention.

as for the “Web does not exist in the namespace System” error. its exactly as it says, simply put because you haven’t loaded the assembly reference to it. which is completely fine as you don’t need that assembly

Yup, should’ve pointed out that I meant whatever class your string extensions live in.

Check namespace as well. If the static class that has your extension method is in a namespace that namespace needs to be imported where you’re using it or it won’t be found.

Ah duh, should’ve known what you meant, haha. That does actually make it compile correctly. Could this just be a new error in the compiler as far as syntax goes? I’d much rather have it work the way I originally had it rather than having to invoke it statically.

If it’s in a static class, with the (this string foo) syntax, and you can’t use it as an extension method, that’s a bug.

Definitely send a bug report with the code that should be compiling not compiling! Those kinds of things should be starting to get fixed now that they’re working on upgrading the language.

I still think you have a class definition or namespace issue. I just tested with 5.5.0p1 and it works just fine.

Here’s the extension method class:

namespace DustinHorne.ExtensionTest
{
 public static class StringExtensions
 {
 public static string ColorText(this string inputString, string colorString)
 {
 return string.Format("<color={0}>{1}</color>", colorString, inputString);
 }
 }
}

And here’s the Monobehavior that outputs to the Console window… which by the way respects the color tag so whatever color you specify as your colorString will actually show up that color in the Unity console. Just attach this to your main camera:

using DustinHorne.ExtensionTest;
using UnityEngine;

public class TestExtensionMethod : MonoBehaviour {

// Use this for initialization
void Start ()
{
 Debug.Log("This is a test string".ColorText("blue"));
}
}

Well Im not using any custom namespaces. All I have is a project-wide static class with the extension method in it. Like: “public static class Extender”. No namespace. Do I have to do “using Extender” in every class I use the extension method in? Is it possible I was doing it wrong but the old compiler didn’t have it implemented correctly but now it is? Ill have to try this when Im back from holiday vacation in a few days.

Id have to see your exact code to know. But no, using is only for the namespace. You should also use one. Not using one pollutes the global namespace and creates possible naming collisions if someone else uses a class called Extender in their asset.