0
\$\begingroup\$

So, here's my issue. I have a class that contains a struct. This struct contains about 7 different string variables. I can set the values of these variables via a constructor that takes the struct variables and matches them to separate holder variables that have the actual values. However, I'm having issues, getting these values. I need to retrieve them from 7 different InputFields. I've tried to do this already. I ended up having some variables without values and some with the same value. How can I retrieve the text value of the different InputFields and pass them to the 7 different holder variables that will set the values of the struct variables?

Below is the first method I tried, this only resulted in Age actually having a value (Note, I had debugging logs to check the assignments, I left out anything that was deemed unnecessary such as debugging logs and methods that are irrelevant to the problem.). I also set the Collect() method to a Button so when the button was pressed it would collect the data. I also tried replacing all the if statements with a switch case, that yielded the same result. As further try, I set up Get methods for all the types and attached them to the InputFields themselves to have them return field.text to the variables. This resulted in the aforementioned issue that some had no values and some had the same value as Age.

using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;

namespace Handlers
{
    public class EventHandler : MonoBehaviour
    {
        InputField field;

        public static string f_name;
        public static string l_name;
        public static string p_age;
        public static string p_height;
        public static string p_weight;
        public static string p_hair;
        public static string p_eye;

        private void Awake()
        {
            field = FindObjectOfType<InputField>();
        }
        public void Save()
        {
            if (File.Exists(Application.persistentDataPath + "/PlayerData.dat"))
            {

            }
            else
            {
                BinaryFormatter formatter = new BinaryFormatter();
                FileStream fstream = File.Create(Application.persistentDataPath + "/PlayerData.dat");
                Profile playerData = new Profile();
                formatter.Serialize(fstream, playerData);
                fstream.Close();
            }
        }
        public void Collect()
        {
            if (field.name == "FirstName")
            {
                f_name = field.text;
            }
            if (field.name == "LastName")
            {
                l_name = field.text;
            }
            if (field.name == "Age")
            {
                p_age = field.text;
            }
            if (field.name == "Height")
            {
                p_height = field.text;
            }
            if (field.name == "Weight")
            {
                p_weight = field.text;
            }
            if (field.name == "Hair_Color")
            {
                p_hair = field.text;
            }
            if (field.name == "Eye_Color")
            {
                p_eye = field.text;
            }
        }
    }
    [Serializable]
    class Profile
    {
        struct Player
        {
            public static string firstName;
            public static string lastName;
            public static string age;
            public static string height;
            public static string weight;
            public static string hair_color;
            public static string eye_color;
        }
        public Profile()
        {
            Player.firstName = EventHandler.f_name;
            Player.lastName = EventHandler.l_name;
            Player.age = EventHandler.p_age;
            Player.height = EventHandler.p_height;
            Player.weight = EventHandler.p_weight;
            Player.hair_color = EventHandler.p_hair;
            Player.eye_color = EventHandler.p_eye;
        }
    }
}

Here's an example of the second attempt (just an example as the entire method system is laid out above, and there is no need to repeat the same process over and over):

public void getFN() 
{
    f_name = field.text;
}
public void Collect() 
{
    getFN();
}

getFN() would be placed on the InputField and Collect() would be on the button so that when the button was clicked it would call each individual like style methods of getFN(). (getLN(), getAge(), etc.)This in return, at least in my mind, should have gotten the individual texts but it didn't.

\$\endgroup\$
1
  • \$\begingroup\$ You say you have an attempted solution, which ended up missing or duplicating some values. Can you show us the code for this version? We may be able to help you debug it. Or, even if it needs to be replaced completely, seeing the code can help tell us more about the way your scenario is set up. \$\endgroup\$
    – DMGregory
    Commented Feb 6, 2017 at 1:53

1 Answer 1

2
\$\begingroup\$

You have a single field that is the first InputField found by FindObjectOfType. Apparently in your case it was the age InputField and that's why that was the only variable that was set.

Read your code for the Collect method, if the value of field or field.name is not changing behind your back (it shouldn't be), the code will only enter one of the if clauses (you may aswell have used a switch statement).

Instead you should be using Object.FindObjectsOfType it should give an array of InputField. Then in the Collect method you would iterate over that array, and for each item check the name and set the appropiate variable.

Something like this:

public class EventHandler : MonoBehaviour
{
    InputField[] fields;

    // ...

    private void Awake()
    {
        fields = FindObjectsOfType<InputField>();
    }

    public void Collect()
    {
        foreach (var field in fields)
        {
            // your old code here
            // or use a switch statement
        }
    }
}
\$\endgroup\$
1
  • \$\begingroup\$ You're like a GodSend, that worked perfectly! If I could vote twice I would. \$\endgroup\$
    – Sora
    Commented Feb 6, 2017 at 3:39

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .