【问题标题】:How to check what a randomized list chose?如何检查随机列表选择了什么?
【发布时间】:2019-02-08 19:13:59
【问题描述】:

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

每次“球”游戏对象与该游戏对象碰撞时,颜色都会随机化。我只想将游戏对象的标签设置为它随机化的任何颜色。为了做到这一点,我首先需要检查随机发生器在列表中选择的内容,但我不知道该怎么做。

此视频显示了我完整使用的随机列表代码: 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";
        }

    }

}

【问题讨论】:

  • 请在您的问题中发布所有相关代码,以便它是独立的,不需要我们观看 youtube 视频的上下文。
  • 您的代码应该完全在问题中。我们不需要点击异地查看您的代码。当该视频消失时会发生什么?您的问题对未来的读者毫无用处。
  • 好的,您选择了一种随机颜色并将其存储在c 中。您可以检查c == TintColors[0] 以查看它是否是列表中的第一个。或者你可以直接说transform.gameObject.tag = c.Name;,假设你所有的颜色都被命名为颜色(假设我们在这里谈论的是System.Drawing.Color)。

标签: c# list random


【解决方案1】:

如果您的要求只是将游戏对象的标签设置为您在 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();
    }
}

【讨论】:

    猜你喜欢
    • 2021-11-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-01-11
    • 1970-01-01
    • 1970-01-01
    • 2023-01-04
    • 1970-01-01
    相关资源
    最近更新 更多