简体   繁体   English

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

[英]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.

相关问题 尝试实例化 object 时无法将类型“UnityEngine.GameObject”隐式转换为“UnityEngine.Vector2” - Cannot implicitly convert type 'UnityEngine.GameObject' to 'UnityEngine.Vector2'" While trying to Instantiate an object 为什么不能将类型&#39;UnityEngine.Vector2&#39;隐式转换为&#39;float&#39;? - Why can I not implicitly convert type 'UnityEngine.Vector2' to 'float'? 无法从“对象”转换为“ UnityEngine.Vector2” - Cannot convert from 'object' to 'UnityEngine.Vector2' 无法将浮点数转换为 UnityEngine.Vector2 - Cannot convert float to UnityEngine.Vector2 实例化 2D object 时无法从“UnityEngine.Vector2”转换为“UnityEngine.Transform” - Cannot convert from 'UnityEngine.Vector2' to 'UnityEngine.Transform' when instantiating a 2D object “UnityEngine.Vector2”类型必须可转换为“System.IComparable”<UnityEngine.Vector2> &#39; 将其用作泛型类 Graph 中的参数 &#39;T&#39;<T> - The type 'UnityEngine.Vector2' must be convertible to 'System.IComparable<UnityEngine.Vector2>' to use it as parameter 'T' in generic class Graph<T> 无法将类型“unityengine.vector3”隐式转换为“float” - cannot implicitly convert type 'unityengine.vector3' to 'float' 无法将类型int&#39;隐式转换为unityengine.vector3&#39; - cannot implicitly convert type int' to unityengine.vector3' 我如何解决“错误 CS0029 无法将类型 &#39;UnityEngine.Quaternion&#39; 隐式转换为 &#39;UnityEngine.Vector3” - How do i fix "error CS0029 Cannot implicitly convert type 'UnityEngine.Quaternion' to 'UnityEngine.Vector3" unity 5.3.0f4错误CS0029;无法将类型&#39;UnityEngine.Vector3&#39;隐式转换为&#39;float&#39; - unity 5.3.0f4 error CS0029;cannot implicitly convert type 'UnityEngine.Vector3' to 'float'
相关标签
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM