Hi guys i need some help.

I creat a script who make a random number 1 to 36, and i want to add numbers an block (int[ ] tomb),becaus
not repeat one number twice.But unity say:
" error CS1001: Unexpected symbol `=', expecting identifier"
Some body can help me?, Thanks.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class IDK : MonoBehaviour {

public int ID;
public int currentID;

public int[ ] tomb;

void Start ()
{
do
{
ID = Random.Range(1,36);
currentID = ID;

}while (currentID != ID);

}

void Update () {
do
{
ID = Random.Range(1,36);
currentID = ID;
tomb[ ] = currentID;
}while (currentID != ID);
}
}

This is not a way to get M random numbers without repeating. You should make a sequence of numbers 1,2,3,4, … N, where N more or equals M, and then loop throught that sequnce interchanging numbers at random place, then take frist M numbers.

Also you missing arrays. This tomb[] = currentID; assigns number to array which make no sense. To put something into array you need an index, like this

tomb[0] = currentID;
1 Like

Thats good but tomb doesn’t pick up number of currentID.

And unity showe problem :IndexOutOfRangeException: Array index is out of range.

I think you may need to revisit your logic. Pretend you’re the computer, and with paper and pencil make a table of variables, their value, and the current iteration. How is currentID ever going to be != ID? You explicitly set them equal on the previous line. I believe this will always execute just once. Use Debug.Log to confirm


currentID = ID;
}while (currentID != ID);

1 Like

IndexOutOfRangeException means you’re trying to get somethig what is not in the array. For example, if you created and array for 3 items and trying to push or get 4th item, then you will get IndexOutOfRangeException.

var array = new float[3];

array[0] = Random.value;
array[1] = Random.value;
array[2] = Random.value;

Debug.Log(array[0]);
Debug.Log(array[1]);
Debug.Log(array[2]);
Debug.Log(array[3]);

This example will give you IndexOutOfRangeException at the last line.

Hi Meszaros,

What you are trying to do can be rewritten like this:

  1. Fill a list with numbers
  2. Shuffle the order of numbers
using System.Collections;
using System.Collections.Generic;
using UnityEngine;


public class IDK : MonoBehaviour
{
    public List<int> tombList = new List<int>();

    private void Start()
    {

        populateListWithNumbers(tombList, 36);
        Shuffle<int>(tombList);

    }

    void populateListWithNumbers(List<int> list, int amount)
    {
        for (int i = 1; i <= amount; i++)
        {
            list.Add(i);
        }
    }

    /**
     * Taken from https://discussions.unity.com/t/535113
     */
    public void Shuffle<T>(IList<T> ts)
    {
        var count = ts.Count;
        var last = count - 1;
        for (var i = 0; i < last; ++i)
        {
            var r = UnityEngine.Random.Range(i, count);
            var tmp = ts[i];
            ts[i] = ts[r];
            ts[r] = tmp;
        }
    }


}

I want to create a script who gives different random number(ID) to my GameObjects.

Usually random numbers aren’t used as IDs. Common practice is to have global static counter and use it’s value for generating a new id. Every time new id is generated, the counter is increased so every new id is unique.

I have a 6x6 area and i want random 3 of theis grow up 1(Vector3 (0,1,0)) every 5sec .