Hello, I have got a problem that i have been working on for a while but I can’t figur it out. I’m not a good programmer, so please bear with me. I have a .Json file that is my database and a class which models the database and i put that content in a list. Afterwards I want to use that list in a different class. If I try to use it in said class the new list is emtpy, but if I run playmode the list is full in the editor window.
What I know so far is, that the list in the jsonController class is filled but I guess its call after the shopManager class ran through, which is a problem. Additionally, if I don’t the use the null check I get a argument out of range exception. I guess it’s happening because the list is empty.
Here’s my code:
using System.Collections.Generic;
using System;
using UnityEngine;
[Serializable]
public class jsonDataClass
{
public string Databasename;
public List<itemStatsList> itemStats;
}
[Serializable]
public class itemStatsList
{
public int objectID;
public string objectSlug;
public string itemName;
public int itemPrice;
public string itemDescription;
public string itemURL;
public int Height;
public int Tiefe;
public int Gewicht;
public itemStatsList( int objectID, string objectSlug, string itemName, int itemPrice, string itemDescription, string itemURL, int Height, int Tiefe, int Gewicht)
{
this.objectID = objectID;
this.objectSlug = objectSlug;
this.itemName = itemName;
this.itemPrice = itemPrice;
this.itemDescription = itemDescription;
this.itemURL = itemURL;
this.Height = Height;
this.Tiefe = Tiefe;
this.Gewicht = Gewicht;
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class jsonController: MonoBehaviour {
public string jsonURL;
public static jsonController Instance { get; private set; }
public static List<itemStatsList> shopItems = new List<itemStatsList>();
public jsonDataClass jsnData { get; set; }
public List<itemStatsList> ShopItems()
{
return shopItems;
}
private void Awake()
{
Instance = this;
StartCoroutine(getData());
IEnumerator getData()
{
Debug.Log("wird geladne");
WWW _www = new WWW(jsonURL);
yield return _www;
if (_www.error == null)
{
processJsonData(_www.text);
}
else
{
Debug.Log("URL nicht geladen");
}
}
void processJsonData(string _url)
{
jsnData = JsonUtility.FromJson<jsonDataClass>(_url);
//Debug.Log(jsnData.Databasename);
//Debug.Log(jsnData.itemStats);
foreach (itemStatsList x in jsnData.itemStats)
{
shopItems.Add(new itemStatsList(x.objectID, x.objectSlug, x.itemName, x.itemPrice, x.itemDescription, x.itemURL, x.Height, x.Tiefe, x.Gewicht));
//Debug.Log(x.objectID);
//Debug.Log(x.itemURL);
}
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Linq;
public class ShopItemManger : MonoBehaviour
{
public jsonController myjsonController;
public List<itemStatsList> shopItems2;
public GameObject[] shopObjekt;
private void Awake()
{
}
// Start is called before the first frame update
void Start()
{
myjsonController = new jsonController();
myjsonController = GameObject.Find("Json Controller").GetComponent<jsonController>();
shopItems2 = new List<itemStatsList>();
shopItems2 = myjsonController.ShopItems();
for (int i = 0; i < shopObjekt.Length; i++)
{
Debug.Log("Player Number " + i + " is named " + shopObjekt*.name);*
}
//Debug.Log("bis hier " + myjsonController.ShopItems());
if (shopItems2 != null && shopItems2.Any())
{
Debug.Log("bis hier " + shopItems2[0].objectID);
}
Debug.Log("In der list ist noch nichts drin " );
// Debug.Log("In der list ist noch nichts drin "+ jsonController.shopItems[0].objectID);
}
Here’s a Screenshot of what it looks like in unity: https://ibb.co/RYf0fsw
(Sorry, it wouldn’t load the picture)
I’m sry some of the Debug logs are in German, but I hope you can understand whats going on anyway.
I appreciate you guys taking your time and am grateful for any help.