Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

426
Views
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"
    }
  ]
}
over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

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);
    }
}
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!