Sign In Register

How can we help you today?

Start a new topic
Answered

How to get object id in unity

 Hi,


i have my objects saved like this: https://puu.sh/tAW8H/2e4cff752a.png


I know how i can get each value (rang, set, hp etc), except for _id.


What i am trying to do is to write a delete function of the object. But i need to get the object id first, to delete it.



 List<GameSparks.Core.GSData> data = response.ScriptData.GetGSDataList("player_Data");
 List<Runes> runeList = new List<Runes>();


for (int i = 0; i < data.Count; i++) {


  //this is how i get each value
  int r = (int)data[i].GetInt("rang");
  int h = (int)data[i].GetInt("hp");
  int s = (int)data[i].GetInt("set");

 
  //here is the error

  int id = (int)data[i].GetGSData("_id").GetInt("$oid");

  print("Get Id:" + id);
  }

How do i get id as an int?


Best Answer

Hi Kelvin,


The issue here seems to be due to a type mismatch. You're attempting to use 'GetInt' on the $oid field, but the value in this field is stored as a string. Could you try this with


string id = data[i].GetGSData("_id").GetString("$oid");


Let us know if this works for you.


Regards,

Vinnie


Answer

Hi Kelvin,


The issue here seems to be due to a type mismatch. You're attempting to use 'GetInt' on the $oid field, but the value in this field is stored as a string. Could you try this with


string id = data[i].GetGSData("_id").GetString("$oid");


Let us know if this works for you.


Regards,

Vinnie

Yes that worked.

Follow up question: How can i change an object with the same object id via cloudcode? I basicly just want to set "visible" on the object to false

var playerDataList = Spark.runtimeCollection("Runes");
  playerDataList.update({
  "$oid": Spark.getData().deleteID
  },
  {
  "$set": {"visible": false}
  },
  false,
  false
  );

error is:
{
 "@class": ".LogEventResponse",
 "error": {
  "deleteID": "NOTNUMERIC"
 }
}

 

 Assuming that 'deleteID' is a valid objectID, the query would be...

playerDataList.update({ "_id" : { "$oid": Spark.getData().deleteID }} );

Login to post a comment