Cannot Assign Void to an Implicitly-Typed Variable

by Michael Sacco Published September 5, 2023
The cover for Cannot Assign Void to an Implicitly-Typed Variable

Introduction

I ran into this error early on in my Unity journey. I’d do a quick write-up to share my thoughts. Whether you’re a seasoned developer or new to Unity, you’ve encountered this error: “Cannot assign void to an implicitly-typed variable.”

It’s one of those error messages that can leave you scratching your head. Fear not. In this guide, I will dive into what you must do to fix this error. Expect a detailed explanation and a practical solution.

Understanding the Error Message

“Cannot assign void to an implicitly-typed variable.” Uh oh! This error is an indicator of a type mismatch in your code.

It would help if you had some context before we dive into solving this C# error.

C# is a statically typed language. C# requires you to declare the specific data type for each variable. The compiler checks the data type during compile-time. In contrast, JavaScript is a dynamically typed language. JavaScript does not require you to declare the specific data type for each variable. And it only checks types during run-time. Boo, JavaScript.

In C#, you can write int myNumber = 8. But you can’t write string myNumber = 8. ‘8’ isn’t a number, so the compiler will throw an error.

An implicitly typed variable uses the var type. Implicitly typed variables infer the type based on context in the compiler. It has a specific set type (like int). But you don’t have to type it. So, you can type int myNumber = 8 and instruct the compiler that it’s an integer. Or, you can write var myNumber = 8, and the compiler will infer that it is an integer for you.

A function with no return value uses the “void” return value. The “void” function return value signifies that the function does not return any value. So, you can’t assign a “void” function to any variable, whether int or a var. But you’ll only get this specific error when assigning Void to var.

When you encounter this error, you are trying to assign a “void” value to an implicitly typed variable. The “void” function return value signifies that the function does not return any value. You can’t assign a value of “nothing” to a variable. You can’t return “nothing” to a variable if the variable depends on “something” to infer its type.

The Importance of Type Safety

Type safety is a fundamental concept in programming languages. Type safety helps catch errors at compile-time rather than run-time. This approach guarantees that your code only uses a variable consistent with the type. In languages like C#, failing to adhere to type safety rules can lead to various compiler errors. One of those errors is the “Cannot assign void to an implicitly-typed variable” error.

When does this error occur?

Now that we’ve established the basics, let’s review the scenario where the error occurs.

In C#, you type an implicit variable using the var keyword. The compiler determines the variable’s data type based on the assigned value. You will encounter this error if you try to set a method that returns “void” to a var-declared variable.

You cannot assign methods that return “void” to a variable.

Resolving the Error

We’ve now identified the common scenarios that lead to this error. Now, let’s discuss strategies to resolve it.

Check the Return Type

Before you assign the result of a function to a variable, check if the return type is compatible. If a method returns “void,” you cannot set it to a variable.

You fix this problem by not assigning the void function to a variable. Call the “void” method, but don’t assign it to a variable.

var myVariable = Update(); // Bad
Update(); // Good

What if I want to return a value from my method?

Suppose you want to assign the result of a method to a variable. In that case, you need to return a variable from the function. Replace the “void” in the method name with the type you want to return. Then, return that value in the method call.

Here is an example:

void Start()
{
  var myBadCode = MyVoidMethod(); // Wrong
  var myNumber = MyIntMethod(); // Right
}

void MyVoidMethod()
{
  // Do nothing
}

int MyIntMethod()
{
  return 8;
}

Practical Example

Let’s illustrate these concepts with a practical example in C#:

// Correct
var myValue = Mathf.Abs(5);

// Error: Cannot assign Void to an implicitly-typed variable
var error = Update();

In the above code, the Mathf.Abs method returns an int value. We can assign that value to an implicitly typed variable. But, the Update method returns “void.” We can’t set “void” to an implicitly typed variable. As a result, the compiler throws an error. (Thanks, Compiler!)

Conclusion

In coding, errors like this are common. But it’s easy to fix. You required a basic understanding of type safety and proper coding practices in c#. You can drop this error from your code by checking the return types of your methods.

Remember, programming is a journey of continuous learning and improvement. Thanks to this guide, you won’t make this mistake again. Build cleaner, faster projects.

Free download: Indie Game Marketing Checklist

Download now

Category

unity errors

Don't forget to share this post!

Popular assets for Unity

See all assets ->
    Buto
    Cutting-edge volumetric fog and volumetric lighting with support for transparent materials.
    Altos
    Volumetric clouds, day night cycles, dynamic skies, global lighting, weather effects, and planets and moons.
    Super Simple Skybox
    A lightweight procedural skybox ideal for semi-stylized projects.
    Gaussian Blur
    Per-pixel gaussian blur on your entire screen, part of the UI, or in-scene objects.
    Outlines
    Image-based Outlines for 2D and 3D games with variable line weight, color, and displacement options.
    Auto Exposure
    Avoid blown-out highlights and too-dark shadows with automatic exposure metering.

Free Indie Game Marketing Checklist

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

Download for free