1

I have 3 separate code that requires me to change the ip address manually whenever I am testing outside. therefore I am trying to enter an inputfield in unity so that I can enter it once and it will be used by all my code but I have no idea how to do it.

enter image description here This is just a simple input field that I have placed in my Homepage.cs page to enter the ip address

using UnityEngine;
using UnityEngine.UI;

public class HomePage : MonoBehaviour
{
public Text playerDisplay;
public InputField ipField;
public Button submitButton;

private void Start()
{
    if (DBManager.LoggedIn)
    {
        playerDisplay.text = "Player: " + DBManager.username;
    }

}

public void QuitGame()
{
    Debug.Log("Quit!");
    Application.Quit();
}
}

This is my Homepage code that I just put the InputField 'ipField' only. The input from here I would like to transfer it to

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;
using UnityEngine.UI;

public class Registration : MonoBehaviour
{
public InputField nameField;
public InputField passwordField;
public Text error1 = null;
public Text error2 = null;

public Button submitButton;

readonly string postUrl = "http://localhost/sqlconnect/register.php";

my Registration.cs page. This is just a part of the code, I only put the relevant part. What I want to replace is the 'localhost' from the readonly string to the inputfield from my Homepage.cs page. Is there a solution to this?

CodingNeeded
  • 111
  • 1
  • 10
  • 1
    Does this answer your question? [Replace host in Uri](https://stackoverflow.com/questions/479799/replace-host-in-uri) – Rhaokiel Feb 03 '20 at 20:09
  • How do i call it from my Registration.cs, because I tried using the unity code by typing string ipAddress = Homepage.findObjectofType<> and there is an error saying i cannot convert it to string – CodingNeeded Feb 03 '20 at 20:48

1 Answers1

1

Yes there are multiple ways. One of them might be using FindObjectOfType in order to get a reference to the Homepage component. Then you can access all its public members like in your case the ipField which is an InputField so you can simply read out its InputField.text

ipAddress = FindObjectOfType<Homepage>().ipField.text;

In case there is only one instance of Homepage anyway.


If possible you should directly reference it in Registration using either a public or [SerializeField] private field like

public class Registration : MonoBehaviour
{
    // Reference this via the Unity Inspector by drag&drop the according GameObject here
    [SerializeField] private Homepage homepage;

    private void Awake()
    {
        // You could still have a fallback here
        if(! homepage) homepage = FindObjectOfType<Homepage>();
    }

    ...
}

and then later simply use

ipAddress = homepage.ipField.text;

Note that FindObjectOfType fails if the according object is inactive or the compomemt disabled!


You can also go fancy and comply with the encapsulation principles by providing public only the very needed thing using a read-only property

public class Homepage : MonoBehaviour
{
    // This still allows to reference the object in the Inspector 
    // but prevents direct access from other scripts
    [SerializeField] private InputField ipField;

    // This is a public ReadOnly property for reading the IP from other scripts
    public string IP => ipField.text;

    ...
}

Then back in the Registartion you would simply use

ipAdress = homepage.IP;

Finally specific for an IP/URL field you could use a Regex to check the input for validity.

derHugo
  • 83,094
  • 9
  • 75
  • 115
  • Thank you so much, took me awhile to understand and implement(bit of a beginner), but this solved it for me. I chose your second solution – CodingNeeded Feb 04 '20 at 17:04
  • 1
    @CodingNeeded glad to help :) the second is actually the better one since `FindObjectOfType` is quite expensive and already referencing stuff via the Inspector prevents this overhead ;) – derHugo Feb 04 '20 at 17:07