每当游戏对象与阵列中的另一个游戏对象发生碰撞时,如何存储每个得分

时间:2017-03-09 03:37:34

标签: c# arrays unity3d

将打出一个盒子的游戏,每个被破坏的盒子的分数将存储在一个数组中。我可以检索每一拳的分数,但我无法将它们存储在一个数组中,以备将来使用。

void OnTriggerEnter(Collider col)
{
    for (int i = 0; i < 10; i++)
    {
        if (col.gameObject.tag == "Punch") // If the box collides with the punch
        {
            Destroy(gameObject);                      //the box will be disappeared
            score[i] = addedValue;                    //put each score in array when the gameobject box is hit

        }
    }

}

1 个答案:

答案 0 :(得分:0)

正如Ted已经评论过,请尝试使用List&lt;&gt;宾语。它是动态的,你不需要设置它的大小。它将在您使用add()方法时进行相应调整。

检索&#34;得分&#34;对象,可以在List上使用getElementAt()方法。

List<Score> scores = new List<Score>();
scores.Add(new Score());
Score aScore = scores.getElementAt(0); // this gets Score located at index 0

如果您不想使用List,可以使用标准数组。

Score[] scores = new Score[100]; // set number of scores in array
scores[0] = new Score();
Score aScore = scores[0];

这似乎不是一个棘手的问题;你会介意用一些代码或你可能收到的错误澄清吗?