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;
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
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
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.
What you are trying to do can be rewritten like this:
Fill a list with numbers
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;
}
}
}
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.