11.6 C
New York
Friday, March 14, 2025

c# – Unity FromJson Utility Not Populating Occasion of Class


I’ve used FromJson in Unity/C# a dozen occasions, and but for some purpose I can’t determine why this one’s not working.

This is the code that does the parsing:

utilizing System.Collections;
utilizing System.Collections.Generic;
utilizing System.IO;
utilizing UnityEngine;

public class ModuleSpawnScript : MonoBehaviour
{

    TextAsset jsonFile;    

    [SerializeField]    
    FaultModules faultList = new FaultModules();

    void Begin()
    {
        jsonFile = Sources.Load("JSON/FaultModules");
        faultList = JsonUtility.FromJson(jsonFile.textual content);
        Debug.Log(faultList.modules[0].identify);
    }
}

The lessons are outlined like so:

utilizing System.Collections;
utilizing System.Collections.Generic;
utilizing UnityEngine;

[System.Serializable]
public class FaultModule
{
    public string identify;
    public int gracePeriod;
    public float multiplier;
    public int cooldown;
}

public class FaultModules
{
    public FaultModule[] modules;
}

And the JSON appears to be like like this:

{ "FaultModules": [
    {
        "name": "genericFaultModule",
        "gracePeriod": 10,
        "multiplier": 0.8,
        "cooldown": 40
    },
    {
        "name": "otherGenericFaultModule",
        "gracePeriod": 15,
        "multiplier": 2,
        "cooldown": 50
    }
] }

The compiler would not throw any errors, and the jsonFile var populates appropriately, however when the Debug line runs, it says that the article reference is not set to an occasion of an object. I get the sensation that it is one thing easy like a lacking semicolon, however for the lifetime of me I do not see it.

The one clue I’ve is that regardless that I used [SerializeField] on faultList, it would not present up within the Inspector.

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Articles