0

Here 's my code. This is designed to get an integer to get the element of an array and use the string as Tags for spawning an object. However, my problem is it usually repeats the random int 3 times consecutively. I wanted to limit the repetition to 2 times consecutively only and still be able to use the integer.

private string[] FirstTags = new string[3]
     {
         "a", "b", "c"
     };
 
 private string[] SecondTags = new string[5]
     {
         "v", "w", "x", "y", "z"
     }
 
     int randomInt_FirstTags;
     int randomInt_SecondTags;
 
 void GetRandomTag()
     {
         randomInt_FirstTags = Random.Range(0, FirstTags.Length);
         randomInt_SecondTags = Random.Range(0, SecondTags.Length);
     }

This code is where I call GetRandomTag() and spawn my objects. It is called every time the player scores.

public IEnumerator SpawnObjects()
         {
             GetRandomTag();
             yield return new WaitForSeconds(0.8f);
             SpawnFirstTags();
             SpawnSecondTags();
         }
Remy
  • 4,843
  • 5
  • 30
  • 60
ZCN
  • 43
  • 1
  • 4
  • 2
    Instead of generating random indexes consider instead to [shuffle](https://stackoverflow.com/q/273313/1997232) the collection and then simply increment the index. – Sinatr Aug 18 '20 at 09:16
  • What is your implementation of `Random.Range()`? – Matthew Watson Aug 18 '20 at 09:26
  • @Sinatr so you're suggesting to shuffle the strings inside my array and not focus on the repeating int that I got from Random.Range(0, Length)? – ZCN Aug 18 '20 at 09:27
  • @MatthewWatson this is my code for SpawnFirstTags() `GameObject screenObject = ObjectPooler.SharedInstance.GetPooledObject(FirstTags[randomInt_FirstTags]);` – ZCN Aug 18 '20 at 09:30
  • Use OOP. Shuffling `List` is basically what you want and then just spawn as many things using `foreach/for` as you need. – Sinatr Aug 18 '20 at 09:32
  • It depends on the context, do you want to spawn infinitely or a limited number? Does your code grow into more complex requirements or is it going to be the same 3 and 5 tags? you can create a limited shuffled list of tags at the beginning, but if you're going for a long run I'd say use an organic noise such as perlin – Bizhan Aug 18 '20 at 09:44
  • Although, creating a shuffled list by itself does not sound like a good idea to me, it really depends on how one implements it. You may use a loop to avoid consecutive occurances but that may be too slow, my bet is still on organic noises – Bizhan Aug 18 '20 at 09:48
  • @Bizhan, *"use a loop to avoid consecutive occurances"* - how would loop help? You need another storage to remember numbers already used.. or just shuffle the list. – Sinatr Aug 18 '20 at 09:51
  • @Sinatr my mistake I just saw the link and the code is fast, there is no need for a loop. – Bizhan Aug 18 '20 at 10:31
  • @Bizhan, I am just wondering how you intend to use loop to check for duplicates. – Sinatr Aug 18 '20 at 10:37
  • @Sinatr first thing that comes to mind is to simply re-random them when they don't match the criteria. Just to note that the OP has two lists, so joining those and creating all the permutations is just about as bad as looping, but for 3x5 that link works better – Bizhan Aug 18 '20 at 10:44
  • @Bizhan _"do you want to spawn infinitely or a limited number?"_ : I want to spawn **infinitely** until the player loses the game. So basically, the spawn gets called whenever the player scores and it stops if the player loses it. _"is it going to be the same 3 and 5 tags"_ : i'll be probably be working with 5 elements for the first array and 18 elements for the second array. That's fixed. – ZCN Aug 18 '20 at 11:58
  • @Sinatr I'm worried shuffling might slow my game down. This function can possibly be called 2000 times if the player reaches 2000 points. I'm looking for a faster and safer way to call out a random element for each array and limiting the repetition to only 2 times consecutively. I don't know if _removing from the list_ can be an option because I have to use the elements throughout the game. What can you suggest? – ZCN Aug 18 '20 at 12:20

0 Answers0