Sign In Register

How can we help you today?

Start a new topic

Unity "object reference not set to an instance of an object"

I'm using GameSparks with unity and I'm trying to pull certain data from the database.


I set up an event called "getItem" with an attribute "type" set to "used in script".


I set up a cloud code event to to access that event via the using the "type" attribute, which will actually access the description field in the data.


    var description = Spark.getData().type; // get the type we passed in

    if(description !== ""){

        // if the type wasnt an empty string, then we can use the type in our query

        Spark.setScriptData('items', Spark.metaCollection('items').find({"description": description}));

    }


In the Test Harness, I authenticate and then go the Log Event with this JSON 


    {

       "@class": ".LogEventRequest",

       "eventKey": "getItem",

       "type": "Sharp"

    }


In the inspector I see Statement Count: 2 with the request and response of


    {

      "@class": ".LogEventResponse",

      "scriptData": {

        "items": [

          {

            "_id": {

              "$oid": "59160a27feeace0001d90f7f"

            },

            "shortCode": "sword",

            "name": "Stone Sword",

            "description": "Sharp",

          }

        ]

      }

    } 


In my Unity code I have everything set up, I authenticate, and on button click it calls this:


   new GameSparks.Api.Requests.LogEventRequest()

    .SetEventKey("getItem")

    .SetEventAttribute("type", "Sharp")

    .Send((response) => {

    if (!response.HasErrors) {

      GSData data = response.ScriptData.GetGSData("items");

      print("Item ID: " + data.GetString("name"));

    } else {

     Debug.Log("Error Saving Player Data...");

    }

   });


Thats when I get a stream of "object reference not set to an instance of an object"


If I remove the print statement it doesn't throw errors. It seems to just not be finding any descriptions of sharp even though the test harness does.


I've tried many variations of the code but can't get it to work.


Ah I see the issue now, my "items" object returns as an array even though theres only one item, I have to foreach through them. Is there a way to return only a single object if I know there will only be one (such as returning by id)?

Hi Justin,


You'd need to check this in your cloud code before returning. If you know for certain your query will only return a single document (e.g. if you're searching by id or another unique field), you could use a  Spark.metaCollection('items').findOne call instead of find. This will return a single object rather than an array.


Alternatively you could perform a check on the result of your find and return a single object or array depending on the amout of matching documents found. Something like this:


var itemDocs = Spark.metaCollection('items').find({"description":description});

if(itemDocs.count()===0)    Spark.setScriptData("items":itemDocs.next());

else                                  Spark.setScriptData("items":itemDocs);


Regards,

Vinnie


Login to post a comment