Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- namespace tslife
- {
- partial class game
- {
- world[] game_intro = _read_world<world>("intro");
- //** other code **//
- public void update()
- {
- //crashes: System.NullReferenceException: Object reference not set to an instance of an object
- Console.WriteLine(game_intro[0].data.Text);
- }
- private static T[] _read_world<T>(string level){
- var json_data = string.Empty;
- string st = "";
- try
- {
- var stream = File.OpenText("Application/story/"+level+".json");
- //Read the file
- st = stream.ReadToEnd();
- }catch(SystemException e){}
- json_data = st;
- //Console.WriteLine(json_data);
- // if string with JSON data is not empty, deserialize it to class and return its instance
- T[] dataObject = JsonConvert.DeserializeObject<T[]>(json_data);
- return dataObject;
- }
- }
- }
- public class worldData {
- public string Text { get; set; }
- public string Icon { get; set; }
- public int sectionID { get; set; }
- }
- public class world
- {
- public worldData data;
- }
- [{
- "world":
- {
- "Text":"Hi",
- "Icon":"image01.png",
- "sectionID": 0
- }
- },
- {
- "world":
- {
- "Text":"Hey",
- "Icon":"image02.png",
- "sectionID": 1
- }
- }
- ]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement