Need advice on instantiating a percentage of gameobjects

Hi,

This is doing my head in but I’d like to know if there’s a better way of doing this. I have 4 types of gameobjects[ ] in an array. I have ‘n’ total numbers of that gameobjects. I’d like to instantiate a “percentage” of those game objects e.g. 25% of n gameobjects. If n objects is 100, then 25 gameobjects should be instantiated. The position and rotation are Random.Range. Here’s my idea.

If using fixed method.

function Start() {
for(var n : int = 0; n<=25, i++) {
//Instantiate GameObject[0];
//Instantiate GameObject[1];
…etc
}
}

If using a percentage method
for(var n : int = 0; n<=(0.25 * numObjects ), i++) {
//Instantiate GameObject[0];
//Instantiate GameObject[1];
…etc
}

The methods works but it feels like it’s badly written. I’d like to know if there’s a better method of using percentages when instantiating objects. Also, in the case scenario if i’d like to give priority to instantiate an object, e.g. 50%, 20%, 20%, 10% or gameobjects, how should i set the range?

Hope that makes sense.

Appreciate the help.

Why not find the percentage before your for statement? Also, can you have an ‘n’ and an ‘I’ in the same line of a for()?

Anyhow, I would do this.

n = someValue/lenghtOfArray;

for(var t : int; t < n; t++)
{}

This way you set the value of the percentage first. Just base it off of the length of the array, under some value.

Thanks Renman3000. If getting a percentage for one value, i think it’s fine. But here’s a scenario I’m having: If i want to have 100 units of gameobjects[ ], let’s say they’re enemies. I have 4 types of enemies which i put them in an array enemies[0], enemies[1] and so on. If i want 50% of the total to be enemies[0], 20% to be enemies[1], 20% to be enemies[2], and 10% to be enemies[3]. How do set the conditions for the range? or should i just make 4 different for loops for each percentage?

A little something I cooked up that might help you.
It creates a mob based on a percentage composition of an Inspector set available enemy type prefabs array.

To use, drop it on a GameObject that you want to represent a group of enemies. Configure the enemyTypes array to point to your source gameobject and / or prefabs. For testing I just linked to some locally created scene objects: cube,sphere,cylinder,capsule but you would use your own models and prefabs of course.

The mob is created by filling two small arrays one of desired enemy types and the other a corresponding amount of percentages. This script doesn’t do anything to enforce or test to see if you specify percentages that are greater than 100%, or that the index of arrays that are being passed in actually are within the bounds of the enemyTypes source array. I leave that up to you.

The initial conditions are in the Start function comments.
Good luck.

#pragma strict
public var enemyTypes : GameObject[];	//Inspector filled array of enemy prefabs.

public var enemyTeam : GameObject[]; 	//Storage for the newly created enemy group

function Start () 
{
//This is the manual configuration of a mob of 100 whose composition is 
//30% cubes, 25% speres, 45% capsules
//assuming enemyTypes[0] = cube_prefab object linked in inspector
//assuming enemyTypes[1] = sphere_prefab object linked in inspector
//assuming enemyTypes[2] = cylinder_prefab object linked in inspector
//assuming enemyTypes[3] = capsule_prefab object linked in inspector


var types : int[]=new int[3];
types[0]=0;//map array percentage 0 to prefab in enemyTypes element 0
types[1]=1;//map array percentage 1 to prefab in enemyTypes element 1
types[2]=3;//map array percentage 3 to prefab in enemyTypes element 3

var spread : float[]=new float[3];
spread[0]=30.0f;//30 percent of type 0 units... 
spread[1]=25.0f;//25 percent of type 1 units... 
spread[2]=45.00;//45 percent of type 2 units...

CreateEnemyMob(100, types, spread);
ShowMobCompostionToDebug();
}

function CreateEnemyMob(mobsize : int, unitTypes : int[], unitSpread:float[])
{
//need to add some checks here.. we're passing in two raw arrays that have to match in size
//and be valid references into the enemyTypes array 

var totalSpreads : int = unitSpread.Length;
var totalTypes : int = unitTypes.Length;

if(totalSpreads != totalTypes)
	{
	//The number of passed in percentages doesn't equal the number of different unitTypes provided
	//can't assign a fixed percentage to the unitstypes provided.
	
	//this would represent bad input and should be dealt with.
	Debug.Log("The number of passed in percentages does not match the number of unit types passed");
	return;
	
	}
	
//reinitialize the enemyTeam to mach the new mob size
enemyTeam = new GameObject[mobsize];
	
var currentSpread : int = 0;
var percentCutoff : float = unitSpread[currentSpread];


var currentUnit : int = 0;
var percentStep : float = 100.0f / mobsize; //each step will be 100% / mobsize 
for(var currentPercent : float = 0.0f; currentPercent < 100.0f; currentPercent += percentStep)
	{
	if(currentPercent>=percentCutoff)
		{
		currentSpread++;
		
		percentCutoff += unitSpread[currentSpread];		
		}
	enemyTeam[currentUnit]=enemyTypes[currentSpread];	
	currentUnit++;	
	}
}

function ShowMobCompostionToDebug()
{
var mobSize : int = enemyTeam.Length;
for (var mobUnitCounter = 0; mobUnitCounter < mobSize; mobUnitCounter++)
	{
	Debug.Log("Unit #"+mobUnitCounter+" is a:"+enemyTeam[mobUnitCounter].name);	
	}
}