我如何跟踪生成了哪个随机选择的模型,以免再次重复?

How can i keep track of which randomly selected model was generated so it won't repeat again?

此代码生成了一个随机游戏对象,但由于只有 9 个游戏对象,有时随机生成的对象会重新生成不止一次。我怎样才能限制它,让一个游戏对象只生成一次?

     public GameObject[] models;
     public static GameObject currentPoint;
     int index;
     public static string randomName;
     public AudioSource FindTheNumber;

public void PlayNumbers()
    {   


        models = GameObject.FindGameObjectsWithTag ("numbers");
        index = Random.Range (0,models.Length);
        currentPoint = models [index];
        randomName = currentPoint.name;
        print ("Trackable " + randomName);
        FindTheNumber.Play ();
        currentPoint.GetComponent<AudioSource> ().PlayDelayed(2);


    }

所以你的问题是,当你调用 currentPoint 并创建一个声音时,你会遇到这样的问题,即 gameObject 可能会在创建 "same" 对象时更频繁地被调用?

如果是,你可以把最后创建的gameObject保存下来,看是否和新的一样?

这样你每次总是有不同的游戏对象 ;)。我不知道你的电话之间的差异是什么以及它们持续多长时间,但检查 gameObject 应该可以解决问题。

如果对象正在执行它的工作,您可以创建一个存储信息的变量。

此外,您可以检查验证这些值是否与当前活动的游戏对象不同。

如果我理解你的问题并更正你的代码应该看起来像这样。

     public GameObject[] models;
 public static GameObject currentPoint;
 int index;
 public static string randomName;
 public AudioSource FindTheNumber;

 public void PlayNumbers()
 {   

    do {
      models = GameObject.FindGameObjectsWithTag ("numbers");
      index = Random.Range (0,models.Length);
      currentPoint = models [index];

   } while(currentPoint.getComponent<AudioSource>().isPlaying);

   randomName = currentPoint.name;  
   print ("Trackable " + randomName);
   FindTheNumber.Play ();
   currentPoint.GetComponent<AudioSource> ().PlayDelayed(2);

 }

现在代码将至少循环一次。如果您尝试使用的对象已经在播放,代码将再次循环遍历数组。 一旦找到未播放的对象,代码将继续执行它应该执行的操作。

如果您遇到任何错误或我的解决方案不正确,请再次 post 并告诉我们您想要什么(详细)

祝你有愉快的一天 ;)

PS:唯一让我烦恼的是,由于 Random.Range...

,它可能会仔细检查某些值

如果我没理解错的话,如何在单独的列表中跟踪所选游戏对象。

    public static List<GameObject> models;
    public static List<GameObject> selectedModels = new List<GameObject>();
    public static GameObject currentPoint;
    int index;
    public static string randomName;
    public AudioSource FindTheNumber;
    public static Random random = new Random();

    public void PlayNumbers()
    {   
        models = GameObject.FindGameObjectsWithTag("numbers").Except(selectedModels).ToList();

        if ((models == null) || (!models.Any()))
        {
            Console.WriteLine("No new game objects");
        }
        else
        {
            index = random.Next(models.Count);
            currentPoint = models[index];
            randomName = currentPoint.name;
            print ("Trackable " + randomName);
            FindTheNumber.Play ();
            currentPoint.GetComponent<AudioSource> ().PlayDelayed(2);

            selectedModels.Add(currentPoint);
        }
}

Vancold.at提出的建议的一种方法

public void PlayNumbers()
{
    var rnd = new Random();
    models = GameObject.FindGameObjectsWithTag("numbers");
    while (true)
    {
        index = rnd.Next(0, models.Length - 1);
        var newPoint = models[index];

        //assumes currentPoint is initially null the first time this method is called
        if(currentPoint == null || newPoint.name != currentPoint.name)
        { 
            currentPoint = newPoint;
            break;
        }
    }
    randomName = currentPoint.name;
    print("Trackable " + randomName);
    FindTheNumber.Play();
    currentPoint.GetComponent<AudioSource>().PlayDelayed(2);
}

请记住,这只能确保同一 GameObject 实例不会连续使用两次。它不会阻止同一对象被多次使用。例如,如果您有 9 个名为“1”、“2”、“3”、“4”、“5”、“6”、“7”、“8”、“9”的 GameObject 实例,则上面的代码允许以下输出:

Trackable 5
Trackable 7
Trackable 5
Trackable 4
Trackable 7

等如果您想在重复之前循环一次可能的值,则需要一种不同的方法。

我找到了解决方案,我会 post 我写的代码,以便将来可以为某人服务:

     public GameObject[] models;
     public static GameObject currentPoint;
     int index;
     public static string randomName;
     public AudioSource FindTheNumber;
     int i =9;

public void PlayNumbers()
    {   


       models = GameObject.FindGameObjectsWithTag ("numbers");
        if (i >= 0) 
        {
            index = i;
            currentPoint = models [index];
            randomName = currentPoint.name;
            print ("Trackable " + randomName);
            FindTheNumber.Play ();
            currentPoint.GetComponent<AudioSource> ().PlayDelayed (2);
            i--;
        } 
        else 
        {
            i=9;
            index = i;
            currentPoint = models [index];
            randomName = currentPoint.name;
            print ("Trackable " + randomName);
            FindTheNumber.Play ();
            currentPoint.GetComponent<AudioSource> ().PlayDelayed (2);
            i--;
        }



    }