[英]Cannot implicitly convert type 'UnityEngine.Vector2?' to 'UnityEngine.Vector2'
This might seem a noob question to ask but I am using gamespark with unity. 这可能看起来像一个noob问题,但我正在使用gamespark与团结。 I am sending vector2 in the pack like this:
我正在这个包中发送vector2:
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? 但我收到以下错误:无法隐式转换类型UnityEngine.Vector2? to UnityEngine.Vector2.
到UnityEngine.Vector2。
The '?' '?' means it is a "nullable" type.
意味着它是一种“可空”的类型。 To convert to a Vector2, do
要转换为Vector2,请执行
Vector2 a = _packet.Data.GetVector2(1).Value;
Here is a link on nullable types from MS: https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/nullable-types/ 以下是来自MS的可空类型的链接: https : //docs.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. Nullable类型还可以选择使用
HasValue
属性检查值是否为null。 If you aren't certain that GetVector2
actually returns a valid value, you should check HasValue
and respond appropriately. 如果您不确定
GetVector2
实际返回的是有效值,则应检查HasValue
并做出相应的响应。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.