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.