Facebook

Unity Error: Extension Method Must Be Defined in a Non-Generic Static Class

by Michael Sacco Published August 21, 2023

I’ve successfully kept my nose out of extension methods for a few years. As it turns out, I was a fool. They’re actually pretty helpful. I was working with extension methods the other day when Unity suddenly reported an error: “Extension Method Must Be Defined in a Non-Generic Static Class”. Oops! I did some research to understand the problem, and then I put my findings together into a blog post for you.

Long story short:

C# requires you to define an extension method in a non-generic static class. To fix the error, move the extension method to a suitable class. Or, change your class to fit the requirements.

For the curious among us, I’ve written up some more detail on this. What causes the error, details on how to fix it, practical examples, and further reading.

What you’ll learn

An Extension Method is a tool in C#. This tool enables you to extend the capabilities of an existing class. But, you may encounter an error: extension method must be defined in a non-generic static class. In this article, we will inspect this error and walk you through how to fix it.

Extension Method Basics

Extension methods make it possible to extend the capabilities of a class. Instances of that class call these methods as if they were instance methods of that type.

What you need to know

  • C# requires you to define extension methods in non-generic static classes.
  • To fix the error, move the extension method to a non-generic static class.
  • Alternatively, modify the current class to make it non-generic and static.

How to fix the error

  1. Identify the extension method causing the error.
  2. Create a new non-generic static class to house the extension method.
  3. Transfer the method definition to the created class.
  4. Rebuild the project to confirm that you resolved the error.

Practical Example on Using Extension Methods

Imagine you’re developing a Unity game. The game has various features. Let’s just imagine that you need to extend the basic methods available to integer types. But, you’ve encountered this error in your IDE.

What do you do?

Assume that you have a component in your project. Assume you want to call an extension method on integer type.


using UnityEngine;

public class MyExtensionMethodDemo : MonoBehaviour

{

   private void Start()

   {

       int myNumber = 42;

       myNumber.MyExtensionMethod();

   }

}

And so far, this is what you have for your Extension class and Extension method:


using UnityEngine;

// If you use a generic non-static class for your Extension method,

// the code will throw an error

public class MyExtensions<T>

{

   // Define the extension method

   public static void MyExtensionMethod<T>(this T obj)

   {

       Debug.Log("Extension method called on " + obj);

   }

}

This code has two problems.

  1. You are using a generic class. You know this because you have an open <T> constructor).
  2. You are also using a non-static class. You know this because your class is missing the static keyword.

To fix this, we need to make your class non-generic and static. To do so, you need to remove the <T> constructor. You also need to add the static keyword.

Here’s what the correct code looks like


using UnityEngine;

// If you use a non-generic static class for your Extension method,

// the code will work

public static class MyExtensions

{

   // Define the extension method

   public static void MyExtensionMethod<T>(this T obj)

   {

       Debug.Log("Extension method called on " + obj);

   }

}

If you save and run your project, you’ll see that the IDE warning is gone and that your project is working now. yeehaw cowboy.

Application Across C# and Unity

This rule is not unique to Unity. It applies in both classic C# programming as well as Unity. You must adhere to this rule in both.

Be a better game developer

This rule is a constant factor in both C# and Unity. Knowing about this rule now can help you in the future. If you are building traditional applications, knowing this will help you. If you are building for Unity, knowing this will help you. Be a pro, remember the rule.

Further reading

Not much else to say on this topic, but I compiled some resources to learn more about the error and the subject matter.

For more general information on extension methods:

Are you a Unity developer?

I’ll bet. Well, I make graphics packages and other tools for Unity. These tools help you make your game. They help you make it more quickly, and they help it look better when you’re done.

I know that building a game is truly a ton of work. That’s why I share free tools with the community to help beginning game developers make their games. And, I create powerful and easy-to-use assets for Unity for intermediate and advanced game developers, too.

Learn more about what I’ve made and how it can help your project on my assets page: https://occasoftware.com/assets

Free download: Indie Game Marketing Checklist

Download now

Category

unity basics

Don't forget to share this post!

Popular assets for Unity

See all assets ->
    Cutting-edge volumetric fog and volumetric lighting with support for transparent materials.
    Volumetric clouds, day night cycles, dynamic skies, global lighting, weather effects, and planets and moons.
    A lightweight procedural skybox ideal for semi-stylized projects.
    Image-based Outlines for 2D and 3D games with variable line weight, color, and displacement options.
    Per-pixel gaussian blur on your entire screen, part of the UI, or in-scene objects.
    Drag-and-drop ready-to-use ambient, impact, and spell particle effects.

Free Indie Game Marketing Checklist

Learn how to make your game successful with this handy checklist.

Download for free