如何查看随机选择的列表是什么?

时间:2019-02-08 19:13:59

标签: c# list random

我有一个随机设置的颜色列表。列表中有4种颜色。

每次“球”游戏对象与该游戏对象碰撞时,颜色就会随机化。我只想将gameObject的标签设置为随机分配的任何颜色。为了做到这一点,我首先需要检查随机化器在列表中选择了什么,但我不确定该怎么做。

此视频显示了我完整使用的随机列表代码: https://www.youtube.com/watch?v=8Xx6ghSk668

void OnTriggerExit2D(Collider2D col)
{
    if (col.gameObject.name == "ball")
    {
        Color c = TintColors[Random.Range(0, TintColors.Count)];
           //scoretext++ (< haven't set this up yet. Ignore this)
        GetComponent<Renderer>().material.color = c;
           //When you score, the color randomizes again

           //Not sure what to do here v
        if (TintColors(1))
           //Not sure what to do here ^
           //If the randomizer chose blue (1 being the 
           // first element in the list)
           //So I can change the gameoject tag to "blue"
        {
            transform.gameObject.tag = "blue";
        }

    }

}

1 个答案:

答案 0 :(得分:2)

如果您只是想将Game Object的标签设置为在OnTriggerExit2D函数中设置的颜色的名称,那么这很简单:

void OnTriggerExit2D(Collider2D col)
{
    if (col.gameObject.name == "ball")
    {
        Color c = TintColors[Random.Range(0, TintColors.Count)];        
        GetComponent<Renderer>().material.color = c;

        // You have already determined what color is selected.
        // all you need do now is assign the string value of that color to the
        // colliding gameObject's tag property.
        col.gameObject.tag = c.ToString();
    }
}