Non repeating random numbers to instantiate a GameObject(Trophy)

How to generate the non repeating random number to instantiate the game object (Trophy)?

private Vector2 GetTrophyClicked()
{
    Vector3 WorldTop = Camera.main.ViewportToWorldPoint(new Vector3((float)0.1, (float)0.12, Camera.main.nearClipPlane));
    Vector3 WorldBase = Camera.main.ViewportToWorldPoint(new Vector3((float)0.4, min, Camera.main.nearClipPlane));
    Vector2 worldPos = new Vector2(Random.Range(WorldBase.x, WorldTop.x), Random.Range(WorldTop.y, WorldBase.y));
    Vector2 gridPos = SnapToGrid(worldPos);
    return gridPos;
}

Hi Subhit,

You could create a list with all your values. With Random.Range, you access an element at an index.

For example:

// member
List<int> indices = new List<int>() { 0, 1, 2, 3};

// in your method
int i = Random.Range(0, indices.Length);
int n = indices[i];
indices.RemoveAt(i);

Did this help?

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms