Give the player every day an other gift, but randomly... ?

Hello. I have a script here, that gives the player a gift for each day he plays. At the moment, the gift is always +250.000 Money. How can I make a random function that gives the player every day an other gift, like the player gets 300.000 or 400.000 or 100.000 …

How can I do it, that the player recieves every day an other gift but randomly?
HERE IS MY SCRIPT:

_

var lastDay : int = System.DateTime.Now.get_Day();
var show : boolean = false;
var money : int;
var Design1 : GUIStyle;
var giftpic : Texture2D;

function Start () {
    money = PlayerPrefs.GetInt("Total_Money");
    
    if (System.DateTime.Now.get_Day() - lastDay == 1) {
       Debug.Log("Have lotsa money!");
       show = true;
       money += 250000;
       PlayerPrefs.SetInt("Total_Money", money);
    }
}

function Update () {
}

function OnGUI () {
    if(show == true){
        GUI.Box(Rect(-10,-10,820,620),"");
        GUI.Label(Rect(100,300,400,25),"You got a gift! +250000 money");
        GUI.Box(Rect(280,150,300,300), giftpic, GUIStyle.none);
        if(GUI.Button(Rect(350,500,100,40),"Accept gift", Design1)){
            show = false;
        }
    }
}

1 Answer

1

Use Random.Range. This supplies a random number within a min and max value. So you could do it like this. At the start of your script, declare two variables:

var maxMoney : int;
var minMoney : int;

Then in your Start () function:

money += Random.Range(minMoney, maxMoney + 1);

The reason I put the + 1 there is because using int parameters, the maximum parameter is exclusive, meaning it is never chosen. So if you were to, say, make your minMoney variable 10000 and your maxMoney variable 250000, without the + 1, the range of choice for the Random function would be 10000 - 249999. Therefore I added the + 1 so you get full numbers. :stuck_out_tongue:

Also, you’re using my lastDay variable wrong. You need to define lastDay when a session is about to be finished. The way you have it now, lastDay is redefined when the script is initialised and so System.DateTime.Now.get_Day() - lastDay will always equal 0. You should look into OnApplicationQuit() for this.

Hope that helps, Klep

Thank you for the help, but the lastDay variable is working in my game. I tested it many times. Or where to put the script than?

+1 , random int fallout strikes again!

it I do this, I get the EQUALS 0 thing there. Can you write me exactly, where to put the script, also with this random gift? Please

is this also working in Webplayer? my game is only in webplayer

Ah yes, if you look at the docs on it it explicitly says: In the web player it is called when the web view is closed. So, all good then. :)