Search Unity

JsonUtility

Discussion in 'Scripting' started by TUGs_The_Unprofessional_Gamer, Oct 18, 2017.

  1. TUGs_The_Unprofessional_Gamer

    TUGs_The_Unprofessional_Gamer

    Joined:
    Dec 21, 2015
    Posts:
    24
    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:

    Code (CSharp):
    1.         QuestData newQuest = JsonUtility.FromJson<QuestData>(Resources.Load<TextAsset>("Data/QuestData").text);
    2.         for (int i = 0; i > newQuest.quests.Length; i++)
    3.         {
    4.             questDatabase.Add(newQuest.quests[i].id, newQuest.quests[i]);
    5.         }
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using System;
    5.  
    6. public class Quest
    7. {
    8.  
    9.     public int id;
    10.     public string questName;
    11.     public string description;
    12.     public int recipient;
    13.     public Reward reward;
    14.     public Task task;
    15.  
    16.     [Serializable]
    17.     public class Reward
    18.     {
    19.         public float exp;
    20.         public float gold;
    21.         public QuestItem[] items;
    22.     }
    23.  
    24.     [Serializable]
    25.     public class Task
    26.     {
    27.         public int[] talkTo;
    28.         public QuestItem[] items;
    29.         public QuestKill[] kills;
    30.     }
    31.  
    32.     [Serializable]
    33.     public class QuestItem
    34.     {
    35.         public int id;
    36.         public int amount;
    37.     }
    38.  
    39.     [Serializable]
    40.     public class QuestKill
    41.     {
    42.         public int id;
    43.         public int amount;
    44.         public int playerCurrent;
    45.     }
    46. }
    47. [Serializable]
    48. public class QuestData
    49. {
    50.     public Quest[] quests;
    51. }
    Code (JavaScript):
    1. {
    2.     "quests":[
    3.     {
    4.         "id": 0,
    5.         "questName": "QuestName of Quest 0",
    6.         "description": "Description of Quest 0",
    7.         "recipient": 0,
    8.         "reward":    {
    9.             "exp": 100.0,
    10.             "gold": 50.0,
    11.             "items": []
    12.                     },
    13.         "task": {
    14.             "talkTo": [],
    15.             "items": [],
    16.             "kills":    [{
    17.                 "id": 0,
    18.                 "amount": 10,
    19.                 "playerCurrent": 0
    20.                         }]
    21.                 }
    22.     },
    23.  
    24.     {
    25.         "id": 1,
    26.         "questName": "QuestName of Quest 1",
    27.         "description": "Description of Quest 1",
    28.         "recipient": 0,
    29.         "reward":    {
    30.             "exp": 100.0,
    31.             "gold": 50.0,
    32.             "items": []
    33.                     },
    34.         "task": {
    35.             "talkTo": [],
    36.             "items": [],
    37.             "kills":    [{
    38.                             "id": 0,
    39.                             "amount": 10,
    40.                             "playerCurrent": 0
    41.                         }]
    42.                 }
    43.     }
    44.     ]
    45. }
    if i Debug the following: "Resources.Load<TextAsset>("Data/QuestData").text" I get the Data from my Jsonfile

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

    What am I overlooking?
     
  2. McDev02

    McDev02

    Joined:
    Nov 22, 2010
    Posts:
    664
    Don't put too much into one line. Check if the file actually exists:
    Code (CSharp):
    1. var file = Resources.Load<TextAsset>("Data/QuestData");
    2. if (file == null) { Debug.LogError("QuestData not found"); }
    3. else
    4. {
    5.     QuestData newQuest = JsonUtility.FromJson<QuestData>(file.text);
    6. if (newQuest == null) { Debug.LogError("Quest could not be deserialized!"); }
    7. //Do stuff...
    8. }
     
  3. TUGs_The_Unprofessional_Gamer

    TUGs_The_Unprofessional_Gamer

    Joined:
    Dec 21, 2015
    Posts:
    24
    Thanks for your response but literally the first sentence after my code says:

    if i Debug the following: "Resources.Load<TextAsset>("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^^)
     
  4. McDev02

    McDev02

    Joined:
    Nov 22, 2010
    Posts:
    664
    I think that there is nothing else that could be null except the result of ResourceLoad.
     
  5. TUGs_The_Unprofessional_Gamer

    TUGs_The_Unprofessional_Gamer

    Joined:
    Dec 21, 2015
    Posts:
    24
    maybe capslock will make sure you read it:
    I MADE 100% SURE THAT "Resources.Load<TextAsset>("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.
     
  6. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,187
    I think your quest class needs to be [Serializable] as well.
     
  7. TUGs_The_Unprofessional_Gamer

    TUGs_The_Unprofessional_Gamer

    Joined:
    Dec 21, 2015
    Posts:
    24
    OMFG! Ive had that but I removed it to test something else and forgot to bring it back. IT FIXED IT. LOVE YOU! :D
     
  8. McDev02

    McDev02

    Joined:
    Nov 22, 2010
    Posts:
    664
    No need to go nuts. You could have been more precive where the error occured.
     
  9. TUGs_The_Unprofessional_Gamer

    TUGs_The_Unprofessional_Gamer

    Joined:
    Dec 21, 2015
    Posts:
    24
    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.
     
  10. daniel-lee156

    daniel-lee156

    Joined:
    Jan 26, 2016
    Posts:
    5
    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!