Your "xcoor" and "ycoor" variables are actually declared in your getData() method, this means that these variables can only be used inside the method getData().
You can declare them at the top of the class, and then you will be able to use the variables in the Update and set the button position.
edited 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/";
string xcoor;
string ycoor;
// 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);
xcoor = coordinates["x1"];
coor = 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);
}
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…