简体   繁体   中英

Why does the method List.Add isn't 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). So, As Steve Said 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");
            }
        }
    }

Each time you click the button you create a new instance of BoardScript . You should store the boardScript variable as a variable in your class or separate component to ensure the data will persist.

You can do either create only one Instance, like:

private Button B;
BoardScript boardScript = new BoardScript();

then in your void Clicker() method: add item to UserList.

Or: You can create a static variable in your BoardScript class and check for null. If its null, then create a new instance else return an old instance.

The following code will help:

private static BoardScript _uniqueBoardScript;

 private BoardScript()
 {}

 public static BoardScript GetInstance()
 {
     if(_uniqueBoardScript==null)
     { 
         _uniqueBoardScript = new BoardScript();
     }
     return _uniqueBoardScript;
 }

and from your void Clicker()

{
    BoardScript boardScript = BoardScript.GetInstance();
    int Two = 2;
    boardScript.UserList.Add(Two);
    Debug.Log("Added);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM