Hello,
I’ve been trying to script a code so it will randomly generate a number from 1 to 30, then checks to see if that number has been generated before, if so generates a new number and checks it, repeats until 10 numbers are generated, then activates 10 objects that are associated with the numbers, so 1 will activate gameObject One, and 30 will activate gameObject Thirty, so on and so on.
Does anyone have any ideas about how to do this?
I know that var number = Random.Range(1,30); should get my numbers and I could use a bunch of if statements but that would get to big too fast.
Store activated values in a list and check with list.contains(), have a an array of 30 bool that you set to true for activated values and check if (activatedValue[value] == false) etc…
void Start()
{
for(int i = 0; i < 10; i++)
{
int randomNum = Random.range(0,30);
if(numberList.Contains(randomNum))
{
i -= 1;
}
else
{
numberList.Add(randomNum);
}
}
}
thats one way. hope it helps. I havnt tries the code. the i-1 might not work. it might cause an infinite loop or something but it has the basic jist of what you want.
ok you can either create another list of gameObjects and their index will correspond to the index of the int list as long as the list is never altered.
you can make a list of gameObjects the same way
public List objects = new List();
The code works fine for generating the numbers, but now I am having a problem activating the objects, it makes the 10 numbers but it is only activating 5 sometimes not even 5 get activated
#pragma strict
var NumberOfNeededRondomNo:int;
var ArrayOfNumbers:int[];
var gameObjects:GameObject[]; /*<--------------------should be as much as in NumberOfNeededRondomNo field*/
var numberOFobjecttoACTIVATE:int;
private var randomNO:int;
private var Is_the_number_in_the_array:int;
private var ToGetAllRandomNo:int;
function Start(){
ToGetAllRandomNo=NumberOfNeededRondomNo*6;
ArrayOfNumbers+=[randomNO];
storeNUMBERSinArray();
yield WaitForSeconds(2);
dezactivate_all_activate_number_of_them();
}
function Update(){
}
function storeNUMBERSinArray(){
var b:int;
var i:int;
for(i=0;i<ToGetAllRandomNo;i++){randomNO=Random.Range(0,NumberOfNeededRondomNo);/*generates random number to be check*/
if(ArrayOfNumbers.Length< NumberOfNeededRondomNo){/*check the current length of the arrey and if its shorterthen goes farther*/
Is_the_number_in_the_array=ArrayOfNumbers.IndexOf(ArrayOfNumbers,randomNO);/*checks if generated number is already in array,if generated random numer is not in array then Indexof returns -1*/
if(Is_the_number_in_the_array<0){ArrayOfNumbers+=[randomNO];}/*if<0==ArraOfNumbers.IndexOf(ArraOfNumbers,randomNO)=-1 .then adds randomNO to array*/
}}}
function dezactivate_all_activate_number_of_them(){
var i:int=0;
for(i=0;i<gameObjects.Length;i++){
gameObjects[i].active=false;
}
for(i=0;i<numberOFobjecttoACTIVATE;i++){
gameObjects[i].active=true;
}
}