public static ConnectionConstants Load()
 {
     if (File.Exists(Application.dataPath + fileName))
     {
         string saveString = File.ReadAllText(Application.dataPath + fileName);
         ConnectionConstants jsonConnection = JsonUtility.FromJson <ConnectionConstants>(saveString);
         return(jsonConnection);
     }
     else
     {
         ConnectionConstants auxConnection = new ConnectionConstants();
         return(auxConnection);
     }
 }
    void OnEnable()
    {
        ConnectionConstants pidConstants = Load();

        for (int i = 0; i < this.transform.childCount - 1; i++)
        {
            if (this.transform.GetChild(i).transform.GetChild(0).transform.GetChild(0).gameObject.name == "InputField Input Caret")
            {
                placeholder[i] = this.transform.GetChild(i).transform.GetChild(0).transform.GetChild(1).GetComponent <Text>();
            }
            else
            {
                placeholder[i] = this.transform.GetChild(i).transform.GetChild(0).transform.GetChild(0).GetComponent <Text>();
            }
        }
        placeholder[0].text = pidConstants.IP.ToString();
        placeholder[1].text = pidConstants.PORT.ToString();
    }
    void OnEnable()
    {
        if (_instance == null)
        {
            _instance = this;
        }
        Debug.Log("Activado");

        ConnectionConstants connection = ConnectionSettings.Load();
        string Host    = connection.IP;
        int    Port    = connection.PORT;
        String message = "";

        connectionStatus       = GameObject.Find("ConnectionStatus").GetComponent <Image>();
        connectionStatus.color = Color.red;
        receivedJSON           = new ReceivedValue();
        mySocket = new TcpClient();
        //SET UP SOCKET CONNECTION
        try
        {
            Debug.Log("Trying to connect: " + Host + ":" + Port.ToString());
            mySocket.Connect(Host, Port);
            theStream = mySocket.GetStream();
            connectionStatus.color = Color.green;
            message += "Connection ready to write";
        }
        catch (Exception e)
        {
            Debug.Log(e);
            message += "Connection NOT ready to write";
        }
        try {
            clientReceiveThread = new Thread(new ThreadStart(Receive));
            clientReceiveThread.IsBackground = true;
            clientReceiveThread.Start();
            message += " and ready to read";
        }
        catch (Exception e) {
            message += " and NOT ready to read";
        }

        Debug.Log(message);
    }
    public void Save()
    {
        for (int i = 0; i < this.transform.childCount - 1; i++)
        {
            if (this.transform.GetChild(i).transform.GetChild(0).transform.GetChild(0).gameObject.name == "InputField Input Caret")
            {
                input_value[i] = this.transform.GetChild(i).transform.GetChild(0).transform.GetChild(2).GetComponent <Text>();
            }
            else
            {
                input_value[i] = this.transform.GetChild(i).transform.GetChild(0).transform.GetChild(1).GetComponent <Text>();
            }
        }
        ConnectionConstants saveConnection = new ConnectionConstants();

        if (input_value[0].text == "")
        {
            saveConnection.IP = placeholder[0].text;
        }
        else
        {
            saveConnection.IP = input_value[0].text;
        }
        if (input_value[1].text == "")
        {
            saveConnection.PORT = int.Parse(placeholder[1].text);
        }
        else
        {
            saveConnection.PORT = int.Parse(input_value[1].text);
        }

        string jsonConnection = JsonUtility.ToJson(saveConnection);

        File.WriteAllText(Application.dataPath + fileName, jsonConnection);
        Debug.Log("Guardado: " + Application.dataPath + fileName);
        this.GetComponentInParent <Settings>().toggle();
    }