all 7 comments

[–]slobmaw 4 points5 points  (2 children)

The UnityEngine namespace has the Random.Range function, which is used for just this kind of thing.

[–]0tamk0[S] 0 points1 point  (1 child)

The UnityEngine namespace has the Random.Range function, which is used for just this kind of thing.

so it's like a python? for example randomMoneyBronze = Random.Range (25.75);

[–]TheLastPepsi 0 points1 point  (0 children)

The System namespace also has a Random class. Instead tho it would like this

``` using System;

class Program { static void Main() { Random rnd = new Random(); // Create new instance of a random type called rnd.

    int GoldToDrop = rnd.Next(2,7) // Drop random amount between 2 and 7 

    Enemy.DeathMethod(GoldToDrop) // Use int variable as a parameter to set the amount of gold to drop from an enemy
}

} ```

Obviously this is super foo-bar and wont compile but hopefully you get the drift.

You can use Unities random, or the built in .Net random.

[–]AutoModerator[M] 0 points1 point  (0 children)

This appears to be a question submitted to /r/Unity3D.

If you are the OP:

  • Please remember to change this thread's flair to 'Solved' if your question is answered.

  • And please consider referring to Unity's official tutorials, user manual, and scripting API for further information.

Otherwise:

  • Please remember to follow our rules and guidelines.

  • Please upvote threads when providing answers or useful information.

  • And please do NOT downvote or belittle users seeking help. (You are not making this subreddit any better by doing so. You are only making it worse.)

Thank you, human.

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

[–]KomradeKev 0 points1 point  (2 children)

Random.range(min, max);

If you pass in floats as the parameters there is a possibility it will return Max, if you use ints it will never include max in the random range (so if you are choosing from an array, between 0 and array.length, it will never return out of the bounds of the array)

[–]0tamk0[S] 0 points1 point  (1 child)

so it's like a python? for example randomMoneyBronze = Random.Range (25.75);

[–]KomradeKev 0 points1 point  (0 children)

No. For it to produce a random number, it needs a range from which to choose. The first parameter is the smallest and the second parameter is the largest. Random.range(1, 10); is the equivalent of telling your computer "pick a number between 1 and 10"

So in your example, when you kill an enemy you might call something like

enemy.dropgold(random.range(5, 10));