Access循环int并使用另一个函数

时间:2015-04-03 08:23:23

标签: c# loops unity3d

我正在尝试使用循环将我的播放器置于空座位且工作正常但是当玩家离开时我试图将bool设置为false但我似乎无法访问它

  

IndexOutOfRangeException:数组索引超出范围。   GetIn.ExitObject()(在Assets / GetIn.cs:118)GetIn.Update()(at   资产/ GetIn.cs:55)

    public int whatSeat;
    Transform TheObject;

    positions = TheObject.GetComponent<GetInObject>().PosInObect;
    for (whatSeat = 0; whatSeat < positions.Length; whatSeat++)
    {
        if (positions[whatSeat].isOccupied == false)
        {
            transform.parent = positions[whatSeat].pos;
            positions[whatSeat].isOccupied = true;
        }
    }

然后在另一个函数中我想访问相同的whatSeat变量,在退出时将其变为false

positions[whatSeat].isOccupied = false;

但这就是错误突然出现的地方我不知道如何解决这个问题

1 个答案:

答案 0 :(得分:0)

试试这个:

    public int whatSeat;
    Transform TheObject;

    positions = TheObject.GetComponent<GetInObject>().PosInObect;
    for (whatSeat = 0; whatSeat < positions.Length; whatSeat++)
    {
        if (positions[whatSeat].isOccupied == false)
        {
            transform.parent = positions[whatSeat].pos;
            positions[whatSeat].isOccupied = true;
            break;   // Adding break here
        }
    }

退出循环后,whatSeat将保留新占用座位的值。