# Drop rate / rarity

I want to drop items with a rarity factor but only one at a time.

I have this at the moment:

``````int rarity = Random.Range (0, 101);

for(int i = 0; i < theCurrency.Length;i++ )
{

if( rarity >= theCurrency*.DropRarity)*
``````
• ``````  		{*
``````

_ Instantiate(theCurrency*.coinPrefab,transform.position,transform.rotation);_
_
}_
_
}*_
The problem is, if rarity is 0 for example, all items get dropped, instead of just the lowest which has a rarity of 5.
How can i fix it?

``````break;
``````

Basically you are looping through all the items and seeing if they are bigger than zero, if they are spawn them. So once you find an item that satisfies the conditions you need to stop looping. Break stops your for loop.

This seems to be a valid solution

``````int itemWeight = 0;

for(int i = 0; i< theCurrency.Length;i++)
{
itemWeight += theCurrency*.DropRarity;*
``````
• `````` 	}*
``````
• `````` 	int randomValue = Random.Range (0, itemWeight);*
``````
• `````` 	for(int i = 0; i< theCurrency.Length;i++)*
``````
• `````` 	{*
``````

_ if(randomValue <= theCurrency*.DropRarity)_
_
{_
_ Instantiate(theCurrency.coinPrefab,transform.position,transform.rotation);
return;
}
randomValue -= theCurrency.DropRarity;
}*_