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.