I am quite new to Unity and C# coding in general so my question might be silly to some of you but I am currently trying to create a button at the location in which its coordinates are from a json string. I am just don't where to go next from here to archive my goals. How can I use the xcoor from json as input in update? Here is my code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;
using UnityEngine.UI;
using SimpleJSON;
public class JSONcontroller : MonoBehaviour
{
public GameObject obj;
Vector3 buttonPos;
private string url = "http://localhost:3000/coordinates/";
// Start is called before the first frame update
void Start()
{
StartCoroutine(getData());
}
IEnumerator getData()
{
UnityWebRequest request = UnityWebRequest.Get(url);
yield return request.SendWebRequest();
if(request.isNetworkError||request.isHttpError)
{
Debug.LogError(request.error);
yield break;
}
JSONNode coordinates = JSON.Parse(request.downloadHandler.text);
string xcoor = coordinates["x1"];
string ycoor = coordinates["y1"];
//Debug.Log(xcoor);
}
// Update is called once per frame
void Update()
{
buttonPos = new Vector3(xcoor, ycoor, 4f);
if (Input.GetButton("Fire1"))
Instantiate(obj, buttonPos, Quaternion.identity);
}
}
and my sample json file
{
"coordinates": [
{
"id": 1,
"x1": "5",
"y1": "2"
},
{
"id": 2,
"x1": "3",
"y1": "5"
},
{
"id": 3,
"x1": "5",
"y1": "7"
}
]
}