C# 如何防止在IEnumerator方法中销毁添加到列表中的类的实例?

C# 如何防止在IEnumerator方法中销毁添加到列表中的类的实例?,c#,list,unity3d,coroutine,ienumerator,C#,List,Unity3d,Coroutine,Ienumerator,如果你能帮我解决这个问题,我将不胜感激 opponetitem类的字段应该用collectItems中的数据填充 我们对手的项目将列在对手列表中,其数据来自incomingData。我不知道Start例程何时结束collectData方法集progressBar和Update检查opponentList的元素 不幸的是,在方法中实例化的opponetitem对象已不存在,因此列表为空 using UnityEngine; using System; using System.Collection

如果你能帮我解决这个问题,我将不胜感激

opponetitem类的字段应该用collectItems中的数据填充

我们对手的项目将列在对手列表中,其数据来自incomingData。我不知道Start例程何时结束collectData方法集progressBar和Update检查opponentList的元素

不幸的是,在方法中实例化的opponetitem对象已不存在,因此列表为空

using UnityEngine;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;

public class OpponentItem {

    public int item_id ;
    public string item_type;
    public string item_name;
    public int item_star; 
    public int item_bonus;

    public OpponentItem() : base(){

    }

}

public class TestBug : MonoBehaviour {

    public List<OpponentItem> opponentList;
    private float barValue;
    public bool isDone;

    public static string[] collectItems =  new string[5]{

        "item_id=12",
        "item_type=Weapon",
        "item_name=Sword",
        "item_star=2",
        "item_bonus=10",

    };

    public string[] incomingData;

    public float progressBar {

        get{ 

            return barValue;

        }
        set{

            barValue = value;
            isDone = true;
        }

    }

    void Start () {

        isDone = false;

        string joined = System.String.Join("|", collectItems);
        incomingData = new string[5];
        incomingData[0] = joined;
        incomingData[1] = joined;
        incomingData[2] = joined;
        incomingData[3] = joined;
        incomingData[4] = joined;

        opponentList = new List<OpponentItem>();

        StartCoroutine(collectData<OpponentItem>(opponentList, incomingData, collectItems));

    }

    void Update(){

        if (isDone){

            isDone = false;
            Debug.Log(opponentList[1].item_id);

        }
    }

    public IEnumerator collectData<T>(List<T> list, string[] tempArray, string[] queryArray) where T : new() {

        list =  new List<T>(tempArray.Length);

        for(int h = 0; h < tempArray.Length ; h++){

            list.Add(new T()); 

            string[] mybox = new string[queryArray.Length]; 
            mybox = tempArray[h].Split('|');

            for (int k = 0; k < queryArray.Length ; k++){

                string[] inbox = new string[2];
                inbox = mybox[k].Split('=');

                if (list[h].GetType().GetField(inbox[0]).FieldType.FullName == "System.Int32"){

                    list[h].GetType().GetField(inbox[0]).SetValue(list[h], Int32.Parse(inbox[1]));
                    Debug.Log(list[h].GetType().GetField(inbox[0]).GetValue(list[h]));

                }
                else if(list[h].GetType().GetField(inbox[0]).FieldType.FullName == "System.Single"){

                    list[h].GetType().GetField(inbox[0]).SetValue(list[h], Single.Parse(inbox[1]));
                    Debug.Log(list[h].GetType().GetField(inbox[0]).GetValue(list[h]));
                }
                else{

                    list[h].GetType().GetField(inbox[0]).SetValue(list[h], inbox[1]);
                    Debug.Log(list[h].GetType().GetField(inbox[0]).GetValue(list[h]));
                }

            }

        }

        yield return new WaitForSeconds(0.2f);
        progressBar += 0.5f; 
    }


}
使用UnityEngine;
使用制度;
使用系统集合;
使用System.Collections.Generic;
运用系统反思;
公共阶级对立{
公共int项目标识;
公共字符串项_类型;
公共字符串项目名称;
公共国际项目之星;
公共int项目奖金;
public opponetitem():base(){
}
}
公共类TestBug:MonoBehavior{
公开名单反对者;
私人价值;
公共广播电台;
公共静态字符串[]collectItems=新字符串[5]{
“项目id=12”,
“物品类型=武器”,
“物品名称=剑”,
“项目_星=2”,
“物品红利=10”,
};
公共字符串[]输入数据;
公共浮动进度条{
获取{
返回值;
}
设置{
barValue=价值;
isDone=true;
}
}
无效开始(){
isDone=false;
连接的字符串=System.string.Join(“|”,collectItems);
incomingData=新字符串[5];
incomingData[0]=已联接;
incomingData[1]=已联接;
incomingData[2]=已连接;
incomingData[3]=已连接;
incomingData[4]=已连接;
opponentList=新列表();
开始例行程序(收集数据(对手列表、收入数据、收集项目));
}
无效更新(){
中频(isDone){
isDone=false;
Debug.Log(对象列表[1]。项id);
}
}
公共IEnumerator collectData(列表列表,字符串[]tempArray,字符串[]queryArray),其中T:new(){
列表=新列表(tempArray.Length);
for(int h=0;h
这里有一个问题。您的列表将始终为空,因为默认情况下,参数是按值传递的(请参见此链接)

opponentList=newlist();
开始例行程序(收集数据(对手列表、收入数据、收集项目));
分配
list=new list(tempArray.Length)
后,您将项目添加到新的列表引用,但不会添加到传递到collectData()的列表引用中

您有两种选择:

1) 清除列表,而不是重新分配

 public IEnumerator collectData<T>(List<T> list, string[] tempArray, string[] queryArray) where T : new() {

    // list =  new List<T>(tempArray.Length); // ** problem
    list.Clear();  // ** try clearing the list instead

    for(int h = 0; h < tempArray.Length ; h++){
public IEnumerator collectData(列表列表,字符串[]tempArray,字符串[]queryArray),其中T:new(){
//列表=新列表(tempArray.Length);//**问题
list.Clear();//**请尝试清除列表
for(int h=0;h
2) 通过引用传递列表

 opponentList = new List<OpponentItem>();

 StartCoroutine(collectData<OpponentItem>(ref opponentList, incomingData, collectItems));

}

    public IEnumerator collectData<T>(ref List<T> list, string[] tempArray, string[] queryArray) where T : new() {

    list =  new List<T>(tempArray.Length);
opponentList=newlist();
开始例行程序(收集数据(参考对手列表、收入数据、收集项目));
}
公共IEnumerator collectData(参考列表列表,字符串[]tempArray,字符串[]queryArray),其中T:new(){
列表=新列表(tempArray.Length);
 opponentList = new List<OpponentItem>();

 StartCoroutine(collectData<OpponentItem>(ref opponentList, incomingData, collectItems));

}

    public IEnumerator collectData<T>(ref List<T> list, string[] tempArray, string[] queryArray) where T : new() {

    list =  new List<T>(tempArray.Length);