List.Add() Not working?
I have a game developed with unity, this is the code of a button. It should take the public List of the script "BoardScript" and add a number to this list, but, for some reason it isn't working. I have checked that the Button click is working (as you can see, in the code the debug.log yes prints), but clicking it is not adding the number to my list. In the "BoardScript" I added some numbers just to try, and the value adds to the list successfully, so, I guess that the error is somewhere in this script, but I can't figure what can be wrong. Can you please help me?
private Button B;
void Start()
{
B = GetComponent<Button>();
B.onClick.AddListener(Clicker);
}
void Clicker()
{
BoardScript boardScript = new BoardScript();
int Two = 2;
boardScript.UserList.Add(Two);
Debug.Log("Added);
}
// Update is called once
EDIT I almost forgot to tell a detail. I have write Debug.Log(UserList.Count) and The value is 1, even if I added 1 several times as the button script suggests (Its the same script but with different object). This is the BoardScript code, it is a little long.
private int[] PreList = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
private List<int> ButtonList = new List<int>();
public List<int> UserList = new List<int>();
private System.Random Rnd = new System.Random();
public List<int> EndList = new List<int>();
void Randomizer()
{
PreList = PreList.OrderBy(C => Rnd.Next()).ToArray();
foreach (var item in PreList)
{
Debug.Log(item.ToString());
if (item == 1)
{
OneMethod();
}
if (item == 2)
{
TwoMethod();
}
if (item == 3)
{
ThreeMethod();
}
if (item == 4)
{
FourMethod();
}
if (item == 5)
{
FiveMethod();
}
if (item == 6)
{
SixMethod();
}
if (item == 7)
{
SevenMethod();
}
if (item == 8)
{
EightMethod();
}
if (item == 9)
{
NineMethod();
}
}
EndList = PreList.ToList();
}
void OneMethod()
{
//yield return new WaitForSeconds(1);
ButtonList.Add(1);
}
void TwoMethod()
{
ButtonList.Add(2);
}
void ThreeMethod()
{
ButtonList.Add(3);
}
void FourMethod()
{
ButtonList.Add(4);
}
void FiveMethod()
{
ButtonList.Add(5);
}
void SixMethod()
{
ButtonList.Add(6);
}
void SevenMethod()
{
ButtonList.Add(7);
}
void EightMethod()
{
ButtonList.Add(8);
}
void NineMethod()
{
ButtonList.Add(9);
}
void Start ()
{
Randomizer();
string[] the_array = ButtonList.Select(i => i.ToString()).ToArray();
string OrderString = string.Join(", ", the_array);
GameObject.Find("Order").GetComponent<Text>().text = OrderString;
UserList.Add(1);
UserList.Add(5);
}
IEnumerator Waiter()
{
yield return new WaitForSeconds(10);
}
// Update is called once per frame
void Update()
{
string[] the_array = UserList.Select(i => i.ToString()).ToArray();
string OrderString = string.Join(", ", the_array);
Debug.Log(OrderString);
if (UserList.Count == 8)
{
if (UserList == ButtonList)
{
//Sound
BehaviourModel B = new BehaviourModel();
B.Counter++;
if (B.Counter < 10)
{
SceneManager.LoadScene(B.SceneArray[B.Counter]);
}
else if (B.Counter > 10)
{
SceneManager.LoadScene("MainMenuScene");
}
}
else if (UserList != ButtonList)
{
UserList.Clear();
Debug.Log("Fail");
}
}
}
BoardScript boardScript = new BoardScript();
You create new board script every time you call Clicker
. that why your UserList always empty every new click
So, Where should I put the instance? If I put it It will create the same error, right?
BoardScript boardScript = new BoardScript();
void Clicker()
{
int Two = 2;
boardScript.UserList.Add(Two);
Debug.Log("Added);
}
Your answer
Follow this Question
Related Questions
How should I use the `FreeLookCam` script located in the standard assets folder 0 Answers
How to add points upon destroying something . 0 Answers
How to automatically generate a new int with a specific name? 1 Answer
Does anyone have a good script that can be used to make a horse/vehicle rideable? 0 Answers