Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
83 views
in Technique[技术] by (71.8m points)

c# - Unity How to use the variables loaded from json string as input in the update section

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"
    }
  ]
}
question from:https://stackoverflow.com/questions/65898365/unity-how-to-use-the-variables-loaded-from-json-string-as-input-in-the-update-se

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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);
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...