Permalink
Switch branches/tags
Nothing to show
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
68 lines (48 sloc) 1.7 KB
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
// This script contains the function to select the random prize or anything that you want to choose.
// But you get to controll which prize to be selected more and which to be selected least.
public class ControlledRandom : MonoBehaviour {
// Prize frequencies to choose randomly
// The more frequency value the more chance to be selected randomly.
[Range(0,10)]
public int prize1,prize2,prize3,prize4;
// This is just a temparory list to hold the values of the prizes to choose from.
// You'll see in the end what I'm going to do with this list.
List<int> prizeList = new List<int>();
void Start()
{
//Init
Init ();
// Select Prize Randomly ("cough by fooling the player cough").
InvokeRepeating ("SelectRandomPrize", 0, 0.3f);
}
/// <summary>
/// Inititalization of the prize
/// </summary>
void Init()
{
prizeList.Clear ();
//Add every prizes in the list
AddItemsInList (prize1,1);
AddItemsInList (prize2,2);
AddItemsInList (prize3,3);
AddItemsInList (prize4,4);
}
void AddItemsInList(int itemTotal,int prizeNum)
{
// This adds the given prize number in the list itemTotal times which is our frequency for the given prize set in the inpector
// This means the more the frequncy value the more the chance of the item to be selected from the list
for (int count = 0; count < itemTotal; count++)
{
prizeList.Add (prizeNum);
}
}
void SelectRandomPrize()
{
// Now we just have to select the prize number from the list which we have created.
int selectedRandomPrizeNumber = prizeList [Random.Range (0, prizeList.Count)];
Debug.Log ("Prize Number "+selectedRandomPrizeNumber);
}
}