Sign In Register

How can we help you today?

Start a new topic

[Unity3D] player missing attributes right after creation


when i create a new player and login to my game he can not save the information about gold or position because he seems to miss the entrys in the database. But when i create a character and go to Test Harness and run my Save_Player script and login to game right after, then everything works.

Can i "play" a jason right after registration or the very first login and then never call it again (or the values will always be null)? Just to get the entrys in the database?

Here is the cloud code for safe_player:


    var playerDataList = Spark.runtimeCollection("playerData"); // this will get the collection of player data
    var playerID = Spark.getPlayer().getPlayerId(); // first we get the id of the current player
    var playerXP = Spark.getData().XP; // we get the xp input from Unity
    var playerGold = Spark.getData().GOLD; // Get the gold input from Unity
    var playerPosX = Spark.getData().POSX;
    var playerPosY = Spark.getData().POSY;
    var playerPosZ = Spark.getData().POSZ;// and the position input from Unity
    var currentPlayer = {
        "playerID": playerID,
        "playerXP": playerXP,
        "playerGold": playerGold,
        "playerPosX": playerPosX,
        "playerPosY": playerPosY,
        "playerPosZ": playerPosZ
    }; // we construct a new player from the data we are about to input into the player data
        "playerID": playerID
    }, //Looks for a doc with the id of the current player
        "$set": currentPlayer
    }, // Uses the $set mongo modifier to set old player data to the current player data
    true, // Create the document if it does not exist (upsert)
    false // This query will only affect a single object (multi)


Code of my c# script for saving:


   public void SafePlayerData()
        playerRef = this.gameObject;

        new GameSparks.Api.Requests.LogEventRequest()
            .SetEventKey("SAVE_PLAYER") // named in db event use shortcode
            .SetEventAttribute("XP", expPoints.ToString()) // use shortcode
            .SetEventAttribute("GOLD", playerGold.ToString())
            .SetEventAttribute("POSX", playerRef.transform.position.x.ToString())
            .SetEventAttribute("POSY", playerRef.transform.position.y.ToString())
            .SetEventAttribute("POSZ", playerRef.transform.position.z.ToString())
            .Send((response) =>
               if (!response.HasErrors)
                   Debug.Log("Player infos saved in db");
                   Debug.Log("Error while saving player infos in db");


If you need, c# code for registration:


   public void RegisterPlayerBttn()
        Debug.Log("Registering Player");
        new GameSparks.Api.Requests.RegistrationRequest()
            .Send((response) =>
               if (!response.HasErrors)
                   Debug.Log("Player Registered \n User Name: " + response.DisplayName);
                   Debug.Log("Error Registering Player...\n " + response.Errors.JSON.ToString());


i dont mean to necro a year old post but im getting a similar issue. any idea whats causing it?

Got this error in Unity:


NullReferenceException: Object reference not set to an instance of an object
PlayerSafe.<LoadPlayerData>m__10(GameSparks.Api.Responses.LogEventResponse)(at Assests/GlobalScripts/PlayerSafe.cs:68)

Line 68 is in the LoadPlayerData function and i this error shows up when he trys to load the gold (it's the first in the loading list) so he doesn't get the gold in playerData. It's simply not created and thats right because when i login to game i want him to load all data but the data gets created on his first save. So how can i create the playerData on registration?

thank you

Hi Jannick,

You say the event works fine from the Test Harness is that correct? If so this narrows it down to a problem in the client side Unity script. From the look of your script everything seems to be in order, when you run the SafePlayerData() Method are you getting the error that should print if this fails? I would double check the .SetEventKey("SAVE_PLAYER") is SAVE_PLAYER in all capitals the same and the short code for this event?

Yes once a player is Authenticated there should be no issue in running this event straight after a full authentication has happened.

Hope this helps

 - Steve

Login to post a comment