Hey, I’m trying to get a list formed from instantiated input fields so that the info can be counted and/or manipulated. I realize that this should be fairly simple, but it seems like I can find some information about creating fields that are populated from a list, but not as much about doing basically the opposite.
I have input fields that are instantiated by a button press, the fields don’t exist prior to that, then when the user clicks out of the field I would like to have the information that they inputted both stay on the screen, as well as get stored in a list for usage.
I’ve created a single script with functions to create the list, add the item to the list, as well as print a count log for testing. The script itself is stored in an empty handler object, and I’ve attached both the add and print functions as separate OnDeselct events in the ‘prefab’ (copied clone). Of course, the desired outcome would be 1, 2, 3…, but I get a 0 for every instantiation of the field. I’ve also tested on the instantiation button with the same result, so I think that the DeSelect is working correctly.
My guess is that a new list is being created every time and never added to. If so where should the list be created and stored? Or, is this possibly just going to be better off in a foreach loop, or maybe with a ‘done’ button of some sort? Or am I just missing something else totally basic about syntax and structure?
I’m not necessarily looking for specific code, though anything would be appreciated, but mostly looking for at least a conceptual idea of how to go about this. Thanks!
(As an aside, I realize that instead of ‘GameObject’ I may eventually need to switch them to TMP objects to achieve some of my final goals, but at this stage I’m trying to isolate each step and GameObjects have usually seemed more straightforward to test with.)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using TMPro;
public class ListMakerName : MonoBehaviour
{
public List<GameObject> list_Names = new List<GameObject>();
int numberOfNames;
void Start()
{
numberOfNames = list_Names.Count;
}
public void addToList()
{
list_Names.Add(gameObject);
}
public void Prt ()
{
Debug.Log(numberOfNames);
}