Quantcast
Channel: Answers by "ransomink"
Viewing all articles
Browse latest Browse all 80

Answer by ransomink

$
0
0
Because your while loop only creates a random integer once (then breaks), during your for loop, it's setting every integer in your array (randomNumbers) to the same value: randomFace. // if randomFace is equal to the result while ( randomFace (3) equals result (3) ) { // set randomFace to a random integer. This will break the loop randomFace (3) now equals random integer (4) } // result becomes the 'new' randomFace integer result (3) now equals randomFace (4) // iterate through every value inside the randomNumbers array for ( iterate through every value inside the array ) { // 4 values are listed because you stated there were 4 values inside the randomNumbers array array value [0] = randomFace (4) array value [1] = randomFace (4) array value [2] = randomFace (4) array value [3] = randomFace (4) } If you're only going to have 4 — or a set amount of — random integers, you should create an empty array; Check if the length is less than 4, if so, then generate a random integer and add it to the array. The code will continue until there are 4 random integers inside of your array. In order to do this, you must use the normal Javascript Arrays '()' and not the built-in arrays '[]' because their size cannot be changed. You can easily copy a normal array into a builtin array so you can see the random numbers generate inside of the inspector. ////////////////////////////////////////////////// // STORE RANDOM INTEGER IN ARRAY ////////////////////////////////////////////////// // INSPECTOR VARIABLES // public var delayTime : float;// wait time before storing a new random integer (for the purpose of this question) public var range : Vector2;// the minimum and maximum range to use when creating a random number (in your case: 1,5) public var randomInt : int;// the current random integer to be stored inside of the array (I made it public so you can see what random integer was generated. If you use the built-in array method then you can keep this variable private. public var maxArrayLength : int;// the maximum length allowed for the array public var randomIntArray : Array;// an array holding the value of each random integer public var builtinRandomIntArray : int [];// a builtin array used to show the current values of the randomIntArray inside the inspector // PRIVATE VARIABLES // private var resetDelayTime : float;// reference to the delayTime variable // used for initialization function Start () { randomIntArray = new Array ();// create an instance of the array resetDelayTime = delayTime;// set resetDelayTime to the current delayTime } // update is called every frame function Update () { delayTime -= Time.deltaTime;// start the countdown. Subtract delayTime by each second (based on last frame) // if the length of the array is shorter than the maximum allowed if ( randomIntArray.length < maxArrayLength ) { // ... and if the wait period is over if ( delayTime <= 0 ) { // ... create a random integer and add it to the array (holding all the random integers) delayTime = 0;// set the delayTime to 0 to stop it from counting down randomInt = Random.Range ( range.x, range.y );// create the random integer using the values from range randomIntArray.Push ( randomInt );// add the random integer to the (randomIntArray) array builtinRandomIntArray = randomIntArray.ToBuiltin ( int ) as int [];// copy the javascript array into a builtin array (so you can see the random integers inside the inspector) Debug.Log ( randomIntArray );// display the current values of the randomIntArray to the console delayTime = resetDelayTime;// reset the delayTime to restart the countdown (the countdown is used to insert a random integer inside of an array. Used for testing purposes only) } } else { Debug.LogWarning ( "Limit has been rached!" ); delayTime = 0;// set the delayTime to 0 to stop it from counting down return; } } I created a test showing how this could be implemented. Hope this helps...

Viewing all articles
Browse latest Browse all 80

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>