- Home /
Hashset and ArrayList Not Updating Upon Button Click
I am working on a unity program which asks a user to enter a number from a certain Range (let's say 1-16). There is a Hashset which is empty at first and List contains integer from 1-16. When the user enters the number, it should be added to hashset and another list method also give another unique number from the list in the range which is also added to hashset and removes that number from the ArrayList. When all the numbers from range 1-16 are added to Hashset. Then, the program should end.
When I run the first time and click the button, Hashset and List is updated and prints their Count value (size). However, when I click the button the second time or more times, the Hashset and List do not get updated and the Count value remains the same. If anybody could help me to find out what am I doing wrong.
Here is my C# script: using System; using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; using TMPro;
public class Game : MonoBehaviour {
public HashSet<int> set;
public ArrayList list;
public TMP_InputField input;
public TMP_Text player;
public TMP_Text robot;
public TMP_Text remain;
//create Hashset and Arraylist. And, initiate reexist()
public void use()
{
set = new HashSet<int>();
list = CreateList();
rexist();
}
//Check if user input is empty or not and if Hashset size is more than the range 1-16
public void rexist()
{
player.text = input.text;
if (!string.IsNullOrEmpty(player.text))
{
if (set.Count < 16)
{
myList(list, set);
pExist(System.Convert.ToInt32(player.text), set);
input.text = null;
}
else if (set.Count == 16)
{
Debug.Log("all done");
input.text = null;
}
}
else
{
player.text = "Empty enter again";
Debug.Log("empty enter again");
}
}
//Check if input from user is valid and is within the given range
public void pExist(int input, HashSet<int> set)
{
if (isVaid(input))
{
if (!set.Contains(input))
{
set.Add(input);
remain.text = set.Count+"";
}
else
{
remain.text = "Repeat Value";
}
}
else
{
player.text = "Number out of Range";
Debug.Log("Number Out of Range. Enter Again.");
}
}
//To check validity of user input
public static Boolean isVaid(int num)
{
return num >= 1 && num <= 16;
}
//Get random integer from the Arraylist and pass it to check it existence.
public void myList(ArrayList list, HashSet<int> set)
{
arrayExist(list, num: list.IndexOf(UnityEngine.Random.Range(1,list.Count)), set: set);
}
//Check if Hashset does not contain the random interger from ArrayList and if the Hashset contains it, remove that random number. Go back to myList method and get another random number.
public void arrayExist(ArrayList list, int num, HashSet<int> set)
{
if (!set.Contains(num))
{
set.Add(num);
list.Remove(num);
robot.text = num+"";
}
else
{
myList(list, set);
}
}
//Creates Arraylist and add integer from 1-16 in it.
public ArrayList CreateList()
{
ArrayList list = new ArrayList();
for (int i = 1; i <= 16; i++)
{
list.Add(i);
}
return list;
}
}
Answer by KittenSnipes · Sep 29, 2018 at 10:26 AM
@thenewtechno If you are specifically using a certain type then just use lists. No point in using a list that holds objects when yours holds integers. Just do this instead:
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using TMPro;
public class Game : MonoBehaviour
{
public List<int> set;
public List<int> list;
public TMP_InputField input;
public TMP_Text player;
public TMP_Text robot;
public TMP_Text remain;
//create Hashset and Arraylist. And, initiate reexist()
public void Use()
{
set = new List<int>();
list = CreateList();
Rexist();
}
//Check if user input is empty or not and if Hashset size is more than the range 1-16
public void Rexist()
{
player.text = input.text;
if (!string.IsNullOrEmpty(input.text))
{
if (set.Count <= 0)
{
set = new List<int>();
MyList(list, set);
PExist(Int32.Parse(input.text), set);
input.text = "";
}
if (set.Count < 16)
{
MyList(list, set);
PExist(Int32.Parse(input.text), set);
input.text = "";
}
else if (set.Count == 16)
{
Debug.Log("all done");
input.text = "";
}
}
else
{
player.text = "Empty enter again";
Debug.Log("empty enter again");
}
}
//Check if input from user is valid and is within the given range
public void PExist(int input, List<int> tempSet)
{
if (IsVaid(input))
{
if (!tempSet.Contains(input))
{
tempSet.Add(input);
remain.text = tempSet.Count + "";
}
else
{
remain.text = "Repeat Value";
}
}
else
{
player.text = "Number out of Range";
Debug.Log("Number Out of Range. Enter Again.");
}
}
//To check validity of user input
public static bool IsVaid(int num)
{
return num >= 1 && num <= 16;
}
//Get random integer from the Arraylist and pass it to check its existence.
public void MyList(List<int> tempList, List<int> tempSet)
{
Debug.LogError(tempList[UnityEngine.Random.Range(0, tempList.Count - 1)]);
//ArrayExist(tempList, tempList[UnityEngine.Random.Range(0, tempList.Count)], tempSet);
}
//Check if Hashset does not contain the random interger from ArrayList and if the Hashset contains it, remove that random number. Go back to myList method and get another random number.
public void ArrayExist(List<int> tempList, int num, List<int> tempSet)
{
if (tempSet.Count <= 0 || tempSet == null)
{
tempSet.Add(num);
tempList.Remove(num);
robot.text = num + "";
}
if (!tempSet.Contains(num))
{
tempSet.Add(num);
tempList.Remove(num);
robot.text = num + "";
}
else
{
MyList(tempList, tempSet);
}
}
//Creates Arraylist and add integer from 1-16 in it.
public List<int> CreateList()
{
list = new List<int>();
for (int i = 1; i <= 16; i++)
{
list.Add(i);
}
return list;
}
}
Answer by $$anonymous$$ · Oct 04, 2018 at 12:38 PM
@KittenSnipes Thanks for your answer. Appreciated
Your answer
Follow this Question
Related Questions
Combine Children Dictionary in place of Hashtable? 2 Answers
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers