- Home /
Refresh panel with prefab contained value from json that created using array
Hello, therefore this is project for thesis at my Uni. So your help means a lot for me, I might give you a shout out on my thesis.
I made a panel displaying prefab(s) based on data that has given by json. So if json have 3 array in it, it will generate 3 prefabs contained the data from json. My main problem is, I made a button inside the panel, that button supposedly a reset button that can delete all of the data on database(which is works fine) but i have an issue on reloading prefabs based on the data itself(which is null,cause the code to delete the data on DB works fine). Here is my code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using System.Linq;
public class getOrder : MonoBehaviour {
public string json;
public Text display;
public GameObject orderListPrefab;
public GameObject canvasPanel2;
public GameObject parentPanel;
public Button ExitPanel2;
public Button resetBtn;
public GameObject sushiRaycast;
public GameObject utama;
Dictionary<string, string[]> hasil;
IEnumerator Start()
{
string uname = PlayerPrefs.GetString ("namaUser");
WWW w = new WWW ("http://192.168.0.14:8080/sushiresto/getUserOrder.php?nama="+ uname);
yield return w;
json = w.text;
getList();
ExitPanel2.onClick.AddListener(() => closePanel());
resetBtn.onClick.AddListener (() => resetOrder ());
}
[System.Serializable]
public struct UserOrderList
{
public string pesanan;
public string jumlah;
public string harga;
}
[System.Serializable]
public class orderData{
public List<UserOrderList> orderList;
}
public void getList () {
orderData data = JsonUtility.FromJson<orderData>(json);
while (this.transform.childCount > 0) {
Transform c = this.transform.GetChild(0);
c.SetParent (null);
Destroy (c.gameObject);
}
//here is the code that generate prefab dummy on each array on json's have
foreach (UserOrderList getData in data.orderList) {
GameObject listPrefab = (GameObject)Instantiate (orderListPrefab);
listPrefab.transform.SetParent (parentPanel.transform, false);
listPrefab.transform.SetParent (this.transform);
listPrefab.transform.Find ("Nama Sushi").GetComponent<Text> ().text = getData.pesanan;
listPrefab.transform.Find ("Harga").GetComponent<Text> ().text = getData.harga;
listPrefab.transform.Find ("Jumlah").GetComponent<Text> ().text = getData.jumlah;
print (getData.pesanan + " " + getData.jumlah + " " + getData.harga);
}
}
void resetOrder(){
string username = PlayerPrefs.GetString ("namaUser");
string CreateUserURL = "http://192.168.0.14:8080/sushiresto/deleteorder.php?usernamePost=" + username;
WWWForm form = new WWWForm ();
form.AddField ("usernamePost", username);
WWW www = new WWW (CreateUserURL, form);
Debug.Log ("Triggered Tes");
}
void closePanel(){
canvasPanel2.gameObject.SetActive(false);
sushiRaycast.gameObject.SetActive(true);
Debug.Log ("Triggered Close");
}
}
This is the panel and the main prefab inside of it.
This is the panel looks like on play mode
Your answer
![](https://koobas.hobune.stream/wayback/20220612124409im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Script sets same value to other script in all objects instead of just one. 1 Answer
How to detect an object which be in FOV of certain camera ? 1 Answer
Creating a GameObject variable without instantiating it? 1 Answer
Access water4 scripts via my script? 1 Answer
Cannot destroy Component while GameObject is being activated or deactivated 2 Answers