无法隐式转换类型'UnityEngine.Vector2?' 到'UnityEngine.Vector2'

Sam*_*ain 3 c# vector unity-game-engine

这可能看起来像一个noob问题,但我正在使用gamespark与团结.我正在这个包中发送vector2:

data.SetVector2(1, new Vector2(1.0f, 1.0f, 1.0f)); 
Run Code Online (Sandbox Code Playgroud)

我从数据包中得到它,如:

Vector2 a = _packet.Data.GetVector2 (1);
Run Code Online (Sandbox Code Playgroud)

但我收到以下错误:无法隐式转换类型UnityEngine.Vector2?到UnityEngine.Vector2.

ava*_*ant 5

'?' 意味着它是一种"可空"的类型.要转换为Vector2,请执行

Vector2 a = _packet.Data.GetVector2(1).Value;
Run Code Online (Sandbox Code Playgroud)

以下是来自MS的可空类型的链接:https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/nullable-types/

Nullable类型还可以选择使用HasValue属性检查值是否为null .如果您不确定GetVector2实际返回有效值,则应检查HasValue并做出适当的响应.

  • 大!记得接受正确的答案! (2认同)