Get Your Personalized Game Dev Plan Tailored tips, tools, and next steps - just for you.

Cannot Assign Void to an Implicitly-Typed Variable

Posted by Gemma Ellison
./
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.

Create a free account, or log in.

Gain access to free articles, game development tools, and game assets.