C# 如何使用foreach销毁arraylist

C# 如何使用foreach销毁arraylist,c#,C#,我的问题是代码foreach(LightWall中的var-walls)。我尝试只使用另一面墙,但出现了此错误:墙与子块中的声明冲突。摆脱它的唯一方法是制作另一个类似的。如果您试图使用错误的参数进行破坏,请使用wall而不是LightWall GameObject Lightwall; // It's here we store the name tag GameObject LightWalls; //the name of the tag Object[] LightWall;

我的问题是代码
foreach(LightWall中的var-walls)
。我尝试只使用另一面墙,但出现了此错误:
墙与子块中的声明冲突
。摆脱它的唯一方法是制作另一个类似的。

如果您试图使用错误的参数进行破坏,请使用
wall
而不是
LightWall

GameObject Lightwall;   // It's here we store the name tag 
GameObject LightWalls;  //the name of the tag
Object[] LightWall; 

public GameObject wallPrefab;
Collider2D wall;  // this is the wall which is surrounding the player
Collider2D walls; //had to make a new because of this error `wall' conflicts with a declaration in a child block

Vector2 lastWallEnd;

void OnTriggerEnter2D(Collider2D co)
{
    if (co != wall && co.tag != "Power Up")
    {
        healthP1--;

        if (healthP1 == 1)
        {
            transform.position = Vector3.zero;

            var Lightwall = GameObject.FindWithTag("LightWalls");
            foreach (var walls in LightWall) //NullReferenceException: Object reference not set to an instance of an object this is the error that i'm getting: Playermovmnet.OnTriggerEnter2D(UnityEngine.Collider2D co)
            {
                Destroy(Lightwall);
            }

        }

如果您试图使用错误的参数进行销毁,请使用
wall
而不是
LightWall

GameObject Lightwall;   // It's here we store the name tag 
GameObject LightWalls;  //the name of the tag
Object[] LightWall; 

public GameObject wallPrefab;
Collider2D wall;  // this is the wall which is surrounding the player
Collider2D walls; //had to make a new because of this error `wall' conflicts with a declaration in a child block

Vector2 lastWallEnd;

void OnTriggerEnter2D(Collider2D co)
{
    if (co != wall && co.tag != "Power Up")
    {
        healthP1--;

        if (healthP1 == 1)
        {
            transform.position = Vector3.zero;

            var Lightwall = GameObject.FindWithTag("LightWalls");
            foreach (var walls in LightWall) //NullReferenceException: Object reference not set to an instance of an object this is the error that i'm getting: Playermovmnet.OnTriggerEnter2D(UnityEngine.Collider2D co)
            {
                Destroy(Lightwall);
            }

        }

更改<代码>销毁(光墙)至<代码>销毁(墙壁)就像上面提到的Valentin一样是正确的方法,但是正如您在代码注释中所说的那样-NullReferenceException-那么问题在于您的前一行返回了一个空引用“var Lightwall=GameObject.FindWithTag(“LightWalls”);“Change
Destroy(Lightwall”)至<代码>销毁(墙壁)就像上面提到的Valentin一样是正确的方法,但是正如您在代码注释中所说的那样-NullReferenceException-那么问题是在您的前一行中返回了一个空引用“var Lightwall=GameObject.FindWithTag(“LightWalls”);”问题是
var Lightwall=GameObject.FindWithTag(“LightWalls”)光墙是一个游戏对象。所以我应该去掉var Lightwall=GameObject.FindWithTag(“光墙”);如果你摆脱了它,你将如何得到一个要销毁的墙列表?代码正在工作,但当我的玩家重置到原始位置时,墙不会被销毁。这可能意味着在重置玩家位置之前,应该在不同的事件触发器上运行此代码。
var Lightwall=GameObject.FindWithTag(“Lightwall”);
Lightwall是一个游戏对象。所以我应该去掉var Lightwall=GameObject.FindWithTag(“LightWalls”);如果你清除了它,你将如何获得要销毁的墙列表?代码正在工作,但当我的玩家重置到原始位置时,墙不会被销毁。这可能意味着在重置玩家位置之前,此代码应该在不同的事件触发器上运行