Currently, I am new to Unity and new to JSON as well, what I have to do is load a JSON file from the server then use variables that stored in it as a vector 3 to instantiate a button, but currently, I don’t know how can I access it after loading it from the server. So how can I use it as I wanted?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;
using UnityEngine.UI;
using SimpleJSON;
using Newtonsoft.Json;
using System;
public class JSONcontroller : MonoBehaviour
{
public GameObject obj;
Vector3 buttonPos;
private string url = "http://localhost:3000/coordinates/";
string x1;
//string y1;
// Start is called before the first frame update
void Start()
{
StartCoroutine(getData());
Debug.Log(x1);
}
IEnumerator getData()
{
UnityWebRequest request = UnityWebRequest.Get(url);
yield return request.SendWebRequest();
if(request.isNetworkError||request.isHttpError)
{
Debug.LogError(request.error);
}
else
{
Debug.Log("Received" + request.downloadHandler.text);
//string jsonR = System.Text.Encoding.UTF8.GetString(request.downloadHandler.data);
//Debug.Log(jsonR);
var box = JsonUtility.FromJson<List<Root>>(request.downloadHandler.text);
Debug.Log(box);
//var coordinate = JsonConvert.DeserializeObject<List<Root>>(request.downloadHandler.text);
//Debug.Log(coordinate
//List<Root> coors = new List<Root>();
//Debug.Log(coors);
//var t = coordinate
//string x1 = GameObject.Find("x1").GetComponent<Text>();
//x1 = coordinate.x1
//var coordinate = JsonConvert.DeserializeObject<List<Root>>(x1);
//Debug.Log(coordinate.x1);
}
}
public void Getcoor()
{
//xco = xcoor;
//yco = ycoor;
}
public void Createbutton()
{
//float x1
//buttonPos = new Vector3(x1, y1, 4f);
//if (Input.GetButton("Fire1"))
// Instantiate(obj, buttonPos, Quaternion.identity);