3

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.

1
  • Random question: new Vector2(1.0f, 1.0f, 1.0f) why are you passing 3 parameters to a Vector2 which can only store 2 values? Commented Feb 28, 2018 at 20:10

1 Answer 1

6

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.

Sign up to request clarification or add additional context in comments.

1 Comment

I was going to. It said I had to wait atleast 10minutes for it... so yeah. Thanks again

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.