[英]Why does the method List.Add isn't working?
我有一個統一開發的游戲,這是一個按鈕的代碼。 它應該使用腳本“ BoardScript”的公共列表,並在此列表中添加一個數字,但是由於某種原因,它不起作用。 我已經檢查了Button單擊是否起作用(如您所見,在代碼中顯示了debug.log yes),但是單擊它並未將數字添加到我的列表中。 在“ BoardScript”中,我嘗試添加一些數字,並將該值成功添加到列表中,因此,我猜該腳本中的某個地方有錯誤,但是我無法弄清楚什么是錯誤的。 你能幫我么?
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
編輯
我差點忘了講一個細節。 我寫了Debug.Log(UserList.Count),即使我按按鈕腳本的建議加了幾次(它是相同的腳本,但對象也不同),但該值還是1。 因此,正如史蒂夫所說,這是BoardScript代碼,它有點長。
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
變量作為變量存儲在類或單獨的組件中,以確保數據將持久存在。
您只能創建一個實例,例如:
private Button B;
BoardScript boardScript = new BoardScript();
然后在您的void Clicker()
方法中:將項目添加到UserList。
或者:您可以在BoardScript類中創建一個靜態變量,然后檢查是否為空。 如果其為null,則創建一個新實例,否則返回一個舊實例。
以下代碼將有所幫助:
private static BoardScript _uniqueBoardScript;
private BoardScript()
{}
public static BoardScript GetInstance()
{
if(_uniqueBoardScript==null)
{
_uniqueBoardScript = new BoardScript();
}
return _uniqueBoardScript;
}
並從您的void Clicker()
{
BoardScript boardScript = BoardScript.GetInstance();
int Two = 2;
boardScript.UserList.Add(Two);
Debug.Log("Added);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.