- Home /
Parse Plugin - Update Object Doesn't Update
I'm trying to update my score on Parse following the tutorial. Every time I try and update it - it just creates a new entry, meaning the player now has two scores. Then I update again, then the user has three scores, etc. The tutorial says this:
// Create the object.
var gameScore = new ParseObject("GameScore")
{
{ "score", 1337 },
{ "playerName", "Sean Plott" },
{ "cheatMode", false },
{ "skills", new List<string> { "pwnage", "flying" } },
};
gameScore.SaveAsync().ContinueWith(t =>
{
// Now let's update it with some new data. In this case, only cheatMode
// and score will get sent to the cloud. playerName hasn't changed.
gameScore["cheatMode"] = true;
gameScore["score"] = 1338;
gameScore.SaveAsync();
});
I assume it creates a new one is because this is all called when updating the score, meaning a new 'gameScore' is created too. But if I remove '`var gameScore = new ParseObject("GameScore")`' I get an error saying 'gameScore doesnt exist in current context'. So I need to define it - but if I do, it will just create a new Object, which completely defeats the purpose of this.
What am I missing here? Thanks
Answer by supernat · May 30, 2014 at 04:16 PM
When you say new ParseObject("GameScore"), it creates a new row entry in the GameScore class (table). If you want to update an existing one, you'll need to first submit a query (see the docs) then use the resulting ParseObject returned from the query (or a list of them), and set the key/value pairs from that. In the example above, if you just want to have a single row for the duration of your game (i.e. add a new row for every game), you can just say "var gameScore = new ParseObject("GameScore") in a script's Awake or Start method, and then update the key/value pairs whenever you want, calling SaveAsync() afterwards.
You only need the lambda expression when you want to do something extra after the async call is made. So really the example they give (the one posted here) is not very useful. You create an entry, set some values, then when it's done loading to the server, you set more values and send it again. It would make more sense to display a modal popup when you start saving async and then remove the popup when the operation is complete, or any other number of things.
By the way, if you use parse analytics or push notifications for mobile, they simply don't work with the Parse Unity plugin, and you have to implement them natively on the mobile. Just FYI.
PARSE PUSH NOW WOR$$anonymous$$S WITH PARSE UNITY PLUGIN. we just have to add ParsePush.jar to the plugin/android folder. ParsePush.jar can be found in PasePush example unity project.
Answer by Trung PV · Jan 02, 2016 at 08:39 AM
I'm late for the party, but in case someone stumble upon this problem, here is what I did:
// Retrive the ParseObject in Start(); query = ParseObject.GetQuery ("HighScore"); query.GetAsync("YourObjectId").ContinueWith(t => { scoreObj = t.Result; });
// At somewhere you want to update the value scoreObj.SaveAsync().ContinueWith(t => { scoreObj["score"] = 5; scoreObj.SaveAsync(); });
Your answer
Follow this Question
Related Questions
Vive HMD tracking does not work 2 Answers
Webplayer Engine Version 7 Answers
Parse Online Storage fails to communicate -1 Answers
How to destroy in an amount of time 1 Answer
using unity string plugin webcam 1 Answer