JsonUtility

Hey guys, Ive been trying to figure out why my code doesnt work for a couple of hours and I just cant fin the misstake:

        QuestData newQuest = JsonUtility.FromJson<QuestData>(Resources.Load<TextAsset>("Data/QuestData").text);
        for (int i = 0; i > newQuest.quests.Length; i++)
        {
            questDatabase.Add(newQuest.quests[i].id, newQuest.quests[i]);
        }
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;

public class Quest
{

    public int id;
    public string questName;
    public string description;
    public int recipient;
    public Reward reward;
    public Task task;

    [Serializable]
    public class Reward
    {
        public float exp;
        public float gold;
        public QuestItem[] items;
    }

    [Serializable]
    public class Task
    {
        public int[] talkTo;
        public QuestItem[] items;
        public QuestKill[] kills;
    }

    [Serializable]
    public class QuestItem
    {
        public int id;
        public int amount;
    }

    [Serializable]
    public class QuestKill
    {
        public int id;
        public int amount;
        public int playerCurrent;
    }
}
[Serializable]
public class QuestData
{
    public Quest[] quests;
}
{
    "quests":[
    {
        "id": 0,
        "questName": "QuestName of Quest 0",
        "description": "Description of Quest 0",
        "recipient": 0,
        "reward":    {
            "exp": 100.0,
            "gold": 50.0,
            "items": []
                    },
        "task": {
            "talkTo": [],
            "items": [],
            "kills":    [{
                "id": 0,
                "amount": 10,
                "playerCurrent": 0
                        }]
                }
    },

    {
        "id": 1,
        "questName": "QuestName of Quest 1",
        "description": "Description of Quest 1",
        "recipient": 0,
        "reward":    {
            "exp": 100.0,
            "gold": 50.0,
            "items": []
                    },
        "task": {
            "talkTo": [],
            "items": [],
            "kills":    [{
                            "id": 0,
                            "amount": 10,
                            "playerCurrent": 0
                        }]
                }
    }
    ]
}

if i Debug the following: “Resources.Load(“Data/QuestData”).text” I get the Data from my Jsonfile

If I debug “JsonUtility.FromJson(Resources.Load(“Data/QuestData”).text)” I get the error “NullReferenceException: Object reference not set to an instance of an object”

What am I overlooking?

Don’t put too much into one line. Check if the file actually exists:

var file = Resources.Load<TextAsset>("Data/QuestData");
if (file == null) { Debug.LogError("QuestData not found"); }
else
{
    QuestData newQuest = JsonUtility.FromJson<QuestData>(file.text);
if (newQuest == null) { Debug.LogError("Quest could not be deserialized!"); }
//Do stuff...
}

Thanks for your response but literally the first sentence after my code says:

if i Debug the following: “Resources.Load(“Data/QuestData”).text” I get the Data from my Jsonfile

So that IS working. It has to do something with the JsonUtility.FromJson(thats at least what i assume^^)

I think that there is nothing else that could be null except the result of ResourceLoad.

maybe capslock will make sure you read it:
I MADE 100% SURE THAT “Resources.Load(“Data/QuestData”).text” IS NOT THE ISSUE!!! I DO GET THE DATA FROM THE JSON-FILE FROM IT!!! Please refrain from replying if youre not willing to determine the issue. Ive told u in the 1. post as well as in my 1.(and now the second as well) reply that I DO GET THE DATA so this issue IS ELIMINATED.

I think your quest class needs to be [Serializable] as well.

1 Like

OMFG! Ive had that but I removed it to test something else and forgot to bring it back. IT FIXED IT. LOVE YOU! :smile:

No need to go nuts. You could have been more precive where the error occured.

Sorry man but from the first reply you were showing that youre not interested in reading.
My very first post is UNEDITED and you can see that I have written this:

In your first 2 answers u fixate on something that I stated IS NOT the issue. I just felt Im talking to a wall.

I cringe when I see data model like this… and that JSON… >.< I don’t mean to be rude but you can really fine tune your data model and the JSON. You can simplify your datamodel and json and reuse a lot of data. Goodluck!