Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot implicitly convert type 'UnityEngine.Vector2?' to 'UnityEngine.Vector2'

This might seem a noob question to ask but I am using gamespark with unity. I am sending vector2 in the pack like this:

data.SetVector2(1, new Vector2(1.0f, 1.0f, 1.0f)); 

and I get it from the packet like:

Vector2 a = _packet.Data.GetVector2 (1);

But I am getting the following error: Cannot implicitly convert type UnityEngine.Vector2? to UnityEngine.Vector2.

like image 699
Sameer Hussain Avatar asked Mar 22 '26 13:03

Sameer Hussain


1 Answers

The '?' means it is a "nullable" type. To convert to a Vector2, do

Vector2 a = _packet.Data.GetVector2(1).Value;

Here is a link on nullable types from MS: https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/nullable-types/

Nullable types also have the option to check if the value is null or not with the HasValue property. If you aren't certain that GetVector2 actually returns a valid value, you should check HasValue and respond appropriately.

like image 125
avariant Avatar answered Mar 24 '26 04:03

avariant