Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

429
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda