What is operator overloading?
- Opetor overloading is use to make math operations like +, -, ==, != ... on your own custom types.
Why use operator overloading?
It’s perfect for custom numeric types, like health, damage, stats, currencies, or game grid positions. Your math operations become short, readable, and less error-prone..
Without operator overloading, you’d often write repetitive Add(), Subtract(), CombineWith() methods. Overloading removes that boilerplate and keeps your API small.
How to use operator overloading?
To overload an operator in C#, you define a public static method inside your class or struct. The method must return a value and use the special operator keyword.
You tell C# which operator you’re overloading by writing it after the operator keyword — like operator + for addition, operator - for subtraction, or operator == for equality.
The operator method must take the same number of operands the operator normally uses. For example, + and - are binary operators, so they take two parameters.
Example use case
Let's see it in action. I’ll write a simple GridPosition struct for demonstration — I think this is one of the most common ways you’d use operator overloading in Unity, especially for tilemaps, grid-based movement, or strategy games.
But you can use the exact same approach for your stat system, prize system, or any other numeric game logic where clean, natural math syntax makes your code easier to read.
CustomGrid struct
The CustomGrid struct is simple — it holds the X and Y coordinates of a grid position. You can use this to represent tile positions, map nodes, or any grid-based system in your game..
public struct CustomGrid
{
public int X { get; }
public int Y { get; }
public CustomGrid(int x, int y)
{
X = x;
Y = y;
}
}
Adding operator overloading
Let's see how we can overload "+" operator.
// + operator requires two parameters
public static CustomGrid operator + (CustomGrid grid1, CustomGrid grid2)
{
return new CustomGrid(grid1.X + grid2.X, grid1.Y + grid2.Y);
}
The method must be public static
It must return a CustomGrid
It takes two CustomGrid parameters — one for each side of the + operation.
Adding other operations
public static CustomGrid operator -(CustomGrid grid1, CustomGrid grid2)
{
return new CustomGrid(grid1.X - grid2.X, grid1.Y - grid2.Y);
}
public static CustomGrid operator *(CustomGrid grid1, CustomGrid grid2)
{
return new CustomGrid(grid1.X * grid2.X, grid1.Y * grid2.Y);
}
// == operator must return a bool
public static bool operator ==(CustomGrid grid1, CustomGrid grid2)
{
return grid1.X == grid2.X && grid1.Y == grid2.Y;
}
// != operator must return a bool
public static bool operator !=(CustomGrid grid1, CustomGrid grid2)
{
return grid1.X != grid2.X || grid1.Y != grid2.Y;
}
- ⚠️ When you overload == and != in C#, you should also override the Equals and GetHashCode methods to ensure your type works correctly in collections and comparisons.
Testing
void Start()
{
CustomGrid grid1 = new CustomGrid(4, 1);
CustomGrid grid2 = new CustomGrid(2, 6);
var addition = grid1 + grid2; // add
var subtraction = grid1 - grid2; // subtract
var multiplication = grid1 * grid2; // multiply element-wise
// override ToString() methods to get expected output
Debug.Log($"Addition: {addition} , subtraction: {subtraction} , multiplication: {multiplication}");
}
output: Addition: (6.00, 7.00) , subtraction: (2.00, -5.00) , multiplication: (8.00, 6.00)
Conclusion
- Operator overloading is a powerful feature in C# that lets you make your custom types behave like built-in ones — using natural and readable syntax.
In Unity development, this means cleaner and more intuitive code for things like grid positions, stats, currencies, and other numeric systems. Instead of writing verbose helper methods, you get to write expressive math operations that improve readability and reduce errors.
Give it a try in your next Unity project, and enjoy the benefits of clearer, cleaner, and more maintainable code!
Thanks for reading
buy me a coffee
Top comments (2)
Great for article, not too long and technical that newcomers won't understand. But also has enough depth for people to learn and implement the concept.
Thanks!