while loop & array- turn on something via percentage

Hello Everyone,

I am currently creating a script that makes rocks fall one at a time by the amount of percentage is complete.

The problem is, it can only be done on very specific numbers. (currently = modulus has to equal zero to turn on falling rock). while the script is able to handle any number of rocks, if I change the speed of how fast the percentage goes up, it can skip rocks, making them fall out of order, or not at all.

Is there a way I can re-write the while loop to have it drop each rock based off of percentage and the number of rocks I have?

The while loop is under the refillRate() method.
Here is my script:

#pragma strict
var rate : float;
var curProgress : float = 0;
private var maxProgress : float = 100;
private var drawGUI = false;
private var percentOfProgress : float;
var progressBarLength : float;
private var maxProgressBarLength : float = 100;

var progressTexture : Texture2D;
var progressTextureMain : Texture2D;
var canDecrease : boolean = false;
//var screenup : float;
//var screenside : float;
var excavateStyle : GUIStyle;
var rocks : Rigidbody[];
private var check : boolean = true;
private var i : int = 0;
private var percentage : float;

function Start () 
{
percentage = (100/rocks.length);
}

function Update () 
{
if (drawGUI == true && Input.GetKey(KeyCode.E))
		{
			refillRate();
		}
else if (canDecrease == true)
		{
		defillRate();
		}

 		percentOfProgress = curProgress/maxProgress; //current % in decimal
		progressBarLength = percentOfProgress*100; // current %
	if (curProgress > maxProgress)
		{
		curProgress = maxProgress;
		}
	if (curProgress < 0)
		{
		curProgress = 0;
		}
 
 
}

function refillRate()
{
		curProgress+= rate/100; 		rocks[0].isKinematic = false;
		
		while(progressBarLength % percentage == 0)
 		{
 		//rocks *= GetComponent.<Rigidbody>().isKinematic = true;*
  •  	if (check == true)*
    
  •  	{*
    
  •  	check = false;*
    
  •  	i++;*
    

_ rocks*.isKinematic = false;_
_
yield WaitForSeconds (0.1);_
_
check = true;_
_
}_
_
else*_
* {*
* Debug.Log(“out of range/time”);*
* }*

* if ( i >= rocks.length-1)*
* {*
* i = 0;*
* }*

}

}
function defillRate()
{
* curProgress-= (rate/100)/2; //fills up the progress meter.*

}
function OnTriggerEnter (theCollider :Collider)
{
* if (theCollider.tag == “Player”)*
* {*
* drawGUI = true;*
* }*
}
function OnTriggerExit (theCollider :Collider)
{
* if (theCollider.tag == “Player”)*
* {*
* drawGUI = false;*
* }*
}

function OnGUI()
{

* if (drawGUI == true)*
* {*
_ GUI.Box (Rect (Screen.width0.5, Screen.height0.3, 300, 45), “Hold E to Excavate”,excavateStyle);
GUI.DrawTexture(Rect((Screen.width/2.5), Screen.height/10, maxProgressBarLength Screen.width/650, 50), progressTextureMain);
GUI.DrawTexture(Rect((Screen.width/2.5), Screen.height/10, progressBarLength Screen.width/650, 50), progressTexture);_
_
}
_

* //calculate bar length, based off of how much is completed.*

}

Yes – your code here is working much too hard. Don’t use coroutines; they’ll just confuse you. Simple keep track of how much “percentage” (work of some sort?) still has to be done before you spawn the next rock. Update this in your Update() method, and when this becomes <= 0, spawn a rock and reset your property to the amount appropriate for the next rock.

No need for while/yield, and no possibility of skipping a rock or spawning them out of order.