0

So to explane it a little bit better, I made a little script that spawns (instanciates) some gameObjects at randomly generated positions. I made it so my z cordinates are randomly generated from 20 to 100 with steps of 10, and x cordinate that can only be 2.2f or -2.2f (but that is not the problem) and everything is done 6 times when function is called. At the end I get (one example) for z cordinates: 30, 30, 60, 50, 40, 60, and repeating values are the problem. How can I make so that no values repeat? Here is my code...

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
 
 public class SpawnerScript : MonoBehaviour
 {
     public GameObject GateSingle;
     public GameObject GateDouble;
     public static SpawnerScript instance;
 
     float RandomXValue;
 
     float minZValue = 20;
     float maxZValue = 100;
     float StepZSize = 10;
 
     void Awake()
     {
         instance = this;
     }
 
     public void Spawn()
     {
         for (int i = 0; i < 6; i++)
         {
             float randomZ = Random.Range(minZValue, maxZValue);
             float numSteps = Mathf.Floor(randomZ / StepZSize);
             float newRandomZ = numSteps * StepZSize;
 
             int RandomXCheck = Random.Range(0, 2);
 
             if (RandomXCheck == 0)
             {
                 RandomXValue = -2.2f;
             }
             else if (RandomXCheck == 1)
             {
                 RandomXValue = 2.2f;
             }
 
             Instantiate(GateSingle, new Vector3(RandomXValue, 0, newRandomZ), Quaternion.identity);
         }
     }
 }
  • make a list of points in order like 10,20,30,40,50,60,70,80 90,100. Then randomize list. Then take values from the randomized list. You will get each point only once. – jdweng Aug 10 '22 at 14:44
  • Does this topic answer your question ? https://stackoverflow.com/questions/30014901/generating-random-numbers-without-repeating-c – ahmet gül Aug 10 '22 at 14:45

0 Answers0