question

Mehdi Boumendjel avatar image
Mehdi Boumendjel asked

InvalidCastException: Specified cast is not valid.

Hi folks,

I've been following a tutorial on implementing PlayFab in Unity, and until now I really love it.

I'm having an issue for cloud functions

Console logs an error :

InvalidCastException: Specified cast is not valid.


PlayFabController.OnCloudUpdateStats (PlayFab.ClientModels.ExecuteCloudScriptResult result) (at Assets/Scripts/PlayFabController.cs:175)
PlayFab.Internal.PlayFabHttp+<>c__DisplayClass23_0`1[TResult].<_MakeApiCall>b__1 () (at Assets/PlayFabSdk/PlayFabSDK/Shared/Internal/PlayFabHttp/PlayFabHTTP.cs:217)
PlayFab.Internal.PlayFabUnityHttp.OnResponse (System.String response, PlayFab.Internal.CallRequestContainer reqContainer) (at Assets/PlayFabSdk/PlayFabSDK/Shared/Internal/PlayFabHttp/PlayFabUnityHttp.cs:245)
UnityEngine.Debug:LogException(Exception)
PlayFab.Internal.PlayFabUnityHttp:OnResponse(String, CallRequestContainer) (at Assets/PlayFabSdk/PlayFabSDK/Shared/Internal/PlayFabHttp/PlayFabUnityHttp.cs:249)
PlayFab.Internal.<Post>d__12:MoveNext() (at Assets/PlayFabSdk/PlayFabSDK/Shared/Internal/PlayFabHttp/PlayFabUnityHttp.cs:196)
UnityEngine.SetupCoroutine:InvokeMoveNext(IEnumerator, IntPtr)

The error brings me to my OnCloudUpdateStats()

private static void OnCloudUpdatePlayerStats(ExecuteCloudScriptResult result)
    {
        // Cloud Script returns arbitrary results, so you have to evaluate them one step and one parameter at a time
        Debug.Log(JsonWrapper.SerializeObject(result.FunctionResult));
        JsonObject jsonResult = (JsonObject)result.FunctionResult;
        object messageValue;
        jsonResult.TryGetValue("messageValue", out messageValue); // note how "messageValue" directly corresponds to the JSON values set in Cloud Script
        Debug.Log((string)messageValue);
    }

From the Documentation : https://api.playfab.com/docs/tutorials/landing-automation/writing-custom-cloud-script

It seems that the Documentation uses the deprecated JsonWrapper and also doesn't seem to mention the need of:

using PlayFab.Json;

EDIT - SOLVE :

I had to replace that line :

Debug.Log(JsonWrapper.SerializeObject(result.FunctionResult));

with

Debug.Log(PlayFab.PluginManager.GetPlugin<ISerializerPlugin>(PluginContract.PlayFab_Serializer));
unity3dCloudScript
1 comment
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Sarah Zhang avatar image Sarah Zhang commented ·

Congratulations, we will accept your solution as the answer.

0 Likes 0 ·

1 Answer

·
Jeremy Rose avatar image
Jeremy Rose answered

This isn't correct either. It should actually be

Debug.Log(PlayFab.PluginManager.GetPlugin<ISerializerPlugin>(PluginContract.PlayFab_Serializer).SerializeObject(result));

To actually serialize the result.

But you can also use this which does the same thing.

Debug.Log(result.ToJson());
,

That isn't really correct either It should be

Debug.Log(PlayFab.PluginManager.GetPlugin<ISerializerPlugin>PluginContract.PlayFab_Serializer).SerializeObject(result));

To actually serialize the result.

But there is also this method which does the same thing.

Debug.Log(result.ToJson());
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.