I have a somewhat overly convoluted class that makes a list of names, and allows you to spin through the names to pick one at random. The issue I’m running into is when I try to remove specific names from the list. I understand my script is probably incredibly sloppy and inefficient, but here it is,
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using TMPro;
public class TextWheelFlooper : MonoBehaviour
{
public Vector3 offset;
public List<MenuObjectMover> textPros;
public List<string> texts;
public List<GameObject> menuObjs;
[SerializeField] bool isMenu;
public Vector2 spinCount;
public float spinDelay;
string indexer;
public TextMeshProUGUI nameObjectForEdit;
public Transform spawnPointForNameEdit;
public float yToAddToSpawnList;
public GameObject nameDeletePanel;
List<TextMeshProUGUI> listOfDeletePanelNames;
bool deleteNamesAreInstantiated;
public TextMeshProUGUI namesList;
bool rouletteSpin;
public int namesAmt = 0;
[SerializeField] private int numberCount = 0;
public TextMeshProUGUI menuObj;
public float lengthMultiplier = 10, numberToAdd;
private Vector3 posGoal;
public GUIFunctionality gUI;
bool init;
void Start()
{
nameDeletePanel.SetActive(false);
posGoal = transform.position;
namesAmt = PlayerPrefs.GetInt("namez" +"A");
if (Screen.width == 1920)
{
offset = new Vector3(950, offset.y, offset.z);
}
else
{
offset = new Vector3(1075, offset.y, offset.z);
}
if (namesAmt > 0)
{
PopulateList("A");
}
init = true;
}
public void PopulateList(string index)
{
indexer = index;
namesList.text = " ";
namesAmt = PlayerPrefs.GetInt("namez" + index);
if(textPros.Count > 0)
{
for (int i = 0; i < textPros.Count; i++)
{
Destroy(textPros[i].gameObject);
}
}
texts = new List<string>(namesAmt);
textPros = new List<MenuObjectMover>();
if (PlayerPrefs.GetString("names" + index + 1) != "")
{
for (int i = 0; i < namesAmt + 1; i++)
{
if (PlayerPrefs.GetString("names" +index + i) != "")
{
texts.Add(PlayerPrefs.GetString("names" +index + i));
}
}
if (texts.Count > 0)
{
for (int i = 0; i < texts.Count; i++)
{
textPros.Add(Instantiate(menuObj.GetComponent<MenuObjectMover>(), transform.position, Quaternion.identity, transform));
menuObjs.Add(textPros[i].GetComponent<MenuObjectMover>().gameObject);
if (!isMenu)
{
textPros[i].GetComponent<TextMeshProUGUI>().text = texts[i];
}
}
textPros[numberCount].textSizeGoal = 1.5f;
}
UpdateWheel();
}
}
private void Update()
{
}
public void CloseDeletePanel()
{
nameDeletePanel.SetActive(false);
}
public void OpenNameDeletePanel()
{
if (deleteNamesAreInstantiated && listOfDeletePanelNames.Count > 0)
{
for (int i = 0; i < listOfDeletePanelNames.Count; i++)
{
Destroy(listOfDeletePanelNames[i].gameObject);
}
}
nameDeletePanel.SetActive(true);
if (texts.Count > 0)
{
listOfDeletePanelNames = new List<TextMeshProUGUI>(texts.Count);
for (int i = 0; i < texts.Count; i++)
{
listOfDeletePanelNames.Add(Instantiate(nameObjectForEdit, spawnPointForNameEdit.position, Quaternion.identity, spawnPointForNameEdit));
listOfDeletePanelNames[i].text = texts[i];
listOfDeletePanelNames[i].transform.position = new Vector3(spawnPointForNameEdit.position.x, spawnPointForNameEdit.position.y - i * yToAddToSpawnList, spawnPointForNameEdit.position.z);
listOfDeletePanelNames[i].GetComponent<NumberAssigner>().index = i;
}
deleteNamesAreInstantiated = true;
}
}
public void RemoveName(int num)
{if (texts[num] != null)
{
texts.Remove(texts[num]);
namesAmt--;
PlayerPrefs.SetInt("namez" + indexer, namesAmt);
SaveNameList();
PopulateList(indexer);
OpenNameDeletePanel();
}
}
public void NumberFlip(int num)
{
numberCount += num;
UpdateWheel();
}
public void UpdateWheel()
{
for (int i = 0; i < textPros.Count; i++)
{
if(i == numberCount)
{
textPros[i].textSizeGoal = 1.5f;
}
else
{
textPros[i].textSizeGoal = 1f;
}
textPros[i].textPosGoal = new Vector3(((numberCount - i)) * numberToAdd + offset.x, transform.position.y, transform.position.z);
}
UpdateNameListText();
}
void SaveNameList()
{
for (int i = 0; i < namesAmt; i++)
{
if (PlayerPrefs.GetString("names" + indexer + i) != "")
{
PlayerPrefs.SetString("names" + indexer + i, texts[i]);
}
}
}
void UpdateNameListText()
{
namesList.text = " ";
if (texts.Count > 0)
{
for (int i = 0; i < texts.Count; i++)
{
if (i != texts.Count - 1)
{
namesList.text += texts[i] + ", ";
}
else
{
namesList.text += texts[i] + "";
}
}
}
}
public void AddRemoveList(string name, bool adding)
{
if (!rouletteSpin)
{
if (adding)
{
texts.Add(name);
textPros.Add(Instantiate(menuObj.GetComponent<MenuObjectMover>(), transform.position, Quaternion.identity, transform));
menuObjs.Add(textPros[textPros.Count - 1].GetComponent<MenuObjectMover>().gameObject);
textPros[textPros.Count - 1].textPosGoal = new Vector3(((numberCount - (textPros.Count - 1))) * numberToAdd + offset.x, transform.position.y, transform.position.z);
textPros[textPros.Count - 1].GetComponent<TextMeshProUGUI>().text = name;
}
else
{
texts.Remove(texts[texts.Count - 1]);
Destroy(menuObjs[menuObjs.Count - 1].gameObject);
Destroy(textPros[textPros.Count - 1]);
textPros.Remove(textPros[textPros.Count - 1]);
menuObjs.Remove(menuObjs[menuObjs.Count - 1]);
}
}
UpdateNameListText();
}
public void SpinRoulette()
{
if (namesAmt > 0)
{
StartCoroutine(RouletteSpin());
}
}
IEnumerator RouletteSpin()
{
rouletteSpin = true;
numberCount = 0;
int spinners;
spinners = (int)Random.Range(spinCount.x, spinCount.y);
for (int i = 0; i < spinners; i++)
{
yield return new WaitForSeconds(spinDelay);
if (numberCount < texts.Count - 1)
{
NumberFlip(1);
}
else
{
NumberFlip(-numberCount);
}
}
rouletteSpin = false;
}
}
So, what I’m trying to do is in the RemoveName method, I want to remove a name from the list, and basically save the list again to player prefs and repopulate the list. It basically deletes everything and then reinstantiates it. But, for some reason, occasionally the list will all be deleted with no errors on clicking a delete button. Also, more importantly, it seems to move one of the names to the top of the list? For example, here I have a list of names I just made, I will delete Jeff.
This is not what I was expecting, but this happened lol
I only deleted the one, yet for some reason the whole list seems to have cleared(i can see in the inspector). It is supposed to be repopulated through player prefs(which looking at it now, the amount of names is still saved to 5 and the names are all there!)
Sorry for the convoluted question, I didn’t even realize as I was writing this there was so much going wrong with my script. Would someone be able to help me figure this out, please?
Thanks in advance
Edit: I just found in my testing that the “indexer” variable gets cleared at some point, which is causing at least one of the problems… I can’t find where though…



