Hey guys, I’m doind a Bingo Game and I want to do a code to generate random numbers only one time.
For example, i will use random = Random.Range(0, 60);
to get a random number, but I need to do something to dont generate this number anymore.
Someone have some idea ?
A list would work here. Store all your bingo numbers in a list, then when a number is picked, remove that number from the list. Run this script on an empty gameObject in a new scene, watch the inspector to see all the numbers being removed from the list :
#pragma strict
// to use List
import System.Collections.Generic;
var numberList : List.< int >;
function Start()
{
numberList = new List.< int >();
// fill the list, store values between 1 and 60
for ( var i : int = 1; i < 61; i ++ )
{
numberList.Add( i );
}
}
function Update()
{
if ( Input.GetKeyDown( KeyCode.Space ) )
{
// check if there are any numbers left
if ( numberList.Count <= 0 )
{
Debug.Log( "All the numbers have been used" );
return;
}
// choose a random position in the list
var rndPos : int = Random.Range( 0, numberList.Count );
// show the chosen number
var nextNumber : int = numberList[ rndPos ];
Debug.Log( "Next Number Is ... " + nextNumber.ToString() );
// remove that item from the list
numberList.RemoveAt( rndPos );
}
}
more information on Generic List : http://wiki.unity3d.com/index.php?title=Which_Kind_Of_Array_Or_Collection_Should_I_Use?#Generic_List