0

I have 4 inputfields, and in each field i need to enter a name. name 1 , name 2 , name 3 , name 4. my issue is that i have a class that get's me the text in the inputfield, and other class that is my manager that handles sending the data i get from the field across the network. My problem is that I can't figure out how to store the all my fields. What i get now in my console is Name 1 = two , name 2 = two. It must be name 1 = one , name 2 = two.

public string name1;
public string name2;
public string name3;
public string name4;


public InputField input;

// Use this for initialization
void Start () {

}

// Called in Inputfield OnEndEdit Event.
public void OnEndEdit()
{
    name1 = input.text;
    name2 = input.text;
    name3 = input.text;
    name4 = input.text;
}

And this is my other script my manager:

    private InputFields inputFields;
    void Start () {

    inputFields = FindObjectOfType<InputFields>();

}
public void GetUserData ()
{
  Debug.Log ("Message 1 " + inputFields.name1 + "Message 2 " + inputFields.name2  + " Message 3 " + "Message 4" ); 
        }
}

So To recap i have 4 UI inputfields and i just want to get text from them and store them in my string. The inputFields script is attached to each inputfield.

David
  • 361
  • 2
  • 3
  • 16
  • I...don't understand. You only have a single `InputField input` declared - if you want to get the values from four different input fields, wouldn't you just declare a public variable for each of them? – Serlite May 18 '17 at 21:48
  • The script is attached to each inputfield and it's public so i drop the inputfield i need. for example script is attached to inputfield 1 and the script takes in a inputfield component. thus i give it inputfield 1. same goes for the others. – David May 18 '17 at 21:49
  • Oh, okay. So are you entering a comma-delimited list of 4 names into each input field? – Serlite May 18 '17 at 21:50
  • What is comma-delimited..? – David May 18 '17 at 21:51
  • Ah, where the items are separated by commas. – Serlite May 18 '17 at 21:51
  • You actually just gave me an idea. I can store them in a array and then read from the array thx! – David May 18 '17 at 21:52
  • You don't understand how Unity works. You attach the script to the input field, so it has the scope of this field. You don't define four names for four input fields, you actually define four names for each input field. So you have a total of 16 name variables in your current code. – UniversE May 18 '17 at 21:53

1 Answers1

1

Use array. Create array of InputField then you can access it from other script with he index from 0 to 3.

The whole string thing you have won't work because when the value of InputField changes, the string won't update the new value. This is why you have to reference the InputField itself which will always have the new value user entered.

public class ScriptB : MonoBehaviour
{
    public InputField[] input;

    public void GetUserData()
    {
        Debug.Log("Message 1: " + input[0].text + "Message 2: " + input[1].text
            + " Message 3: " + input[2].text + "Message 4: " + input[3].text);
    }
}

If you need to register the events, see this post.

Community
  • 1
  • 1
Programmer
  • 121,791
  • 22
  • 236
  • 328
  • Why the array in ScriptA.. i dont understand why it's needed – David May 18 '17 at 21:58
  • I already have it working but with duplications. I don't want to duplicate. – David May 18 '17 at 21:59
  • Yes. but i still don't see what it's needed in scriptA. i can just create it in ScriptB. – David May 18 '17 at 22:00
  • What i want is one functions or simplified code (atleast not have duplications) to store me the 4 strings i will get from my data field so i can send these strings over. – David May 18 '17 at 22:01
  • Sure. You can access that `input` variable directly and access the value. There will be only one copy. Check my updated answer. Also, you can define that variable in `ScriptB` so that you don't have to find the other script at-all. I am doing it like this because you seem to want to access it from another script – Programmer May 18 '17 at 22:05
  • I don't mind doing it all from the same script. I'm just trying to remove the current ugly code i have and for some reason i can get my logic around XD – David May 18 '17 at 22:06
  • That's fine. You know how to do that from one script or do I have to modify the answer again? – Programmer May 18 '17 at 22:07
  • if you can please do :) – David May 18 '17 at 22:07
  • Check the Edited code. I recommend you learn about arrays in C#. – Programmer May 18 '17 at 22:10
  • mmm ye.. What was confusing me is that i though that i must use the inputfield event in order to get the text from it. That's why i was confused. because i was trying to get do everything in one event and avoid duplications. – David May 18 '17 at 22:17
  • I see. You only need the input event if you want to detect when user is done entering stuff and you want to do something right away. If you just want to access the values later on and don't need to know when user is done typing then you don't need the input event. In fact, in this case where you need 4 input fields, you don't need the input event. Just use a submit button that will tell when user is done typing. – Programmer May 18 '17 at 22:21
  • 1
    Yes That's what im doing. Thanks! – David May 18 '17 at 22:22