I would like the game to give all 16 characters (15 NPC + 1 player) a number: 1-16. This number stays the same no matter what and is unique only to that character so two characters cannot have the same number. At a point in the game, the character presses a button.

I need this button to generate a random number and load a scene based on the number. Example: Player's pre-determined number is "8", if the number generator generates an "8" as well, it would load the next scene (where the player has to run away from a mob) If the generator generates anything else other than "8" It will load a different scene (where the player chases an NPC)

Just to clear it up if the number generator generates 1-7 or 9-16 it will load the same scene no matter what. (Getting "9" from the generator would load the same scene as "2" would. Only when "8" is generated, would there be a separate scene)

I'm still incredibly new to Unity and coding in general, so a brief summary of what each line of code is doing would be majorly appreciated! (but not at all demanded!)

I'm most comfortable with C# so if you could have the code in that would be great!

  • \$\begingroup\$ Is the order of the characters important or do you just want to get decision between a NPC and a player related action? \$\endgroup\$
    – Cryptjar
    Oct 24, 2017 at 4:37
  • 2
    \$\begingroup\$ List. Shuffle. Indexes. \$\endgroup\$ Oct 24, 2017 at 16:01
  • \$\begingroup\$ @Cryptjar I just need to get the decision between an NPC and a player \$\endgroup\$ Oct 26, 2017 at 0:04

1 Answer 1


First I will suggest that you google specific things before you ask questions. Such as for the random number you could google "unity random int" or "c# random int". https://docs.unity3d.com/ScriptReference/Random.Range.html https://stackoverflow.com/questions/2706500/how-do-i-generate-a-random-int-number-in-c

Either way you could do this by having an object with a scrip that gives numbers to your characters. You could either find the characters in your script or add them to your number giver object. Let's assume the latter.

// Lists are like arrays but has an Add 
public GameObject[] characters; 
// Lists are like arrays but has an Add function (worth a google)
private List<int> usedNumbers;

void Start() {
    // Random number from 1 to the amount of characters in the array
    int randomNumber = Random.Range(1, characters.Length);
    // Loop that takes every single object in the characters array
    foreach (GameObject character in characters) {
        // Loop until we have a random number that isn't in usedNumbers
        while (usedNumbers.Contains(randomNumber) {
            randomNumber = Random.Range(1, characters.Length);
        // Add the unique random number to usedNumbers list
        // Give number to your character (CharacterController can simply be a script with a public int)
        character.GetComponent<CharacterController>().number = randomNumber;

That script can do the job but I haven't tested it myself as I can't right now.

It will do the job but I'd still advice googling for a method on how to exclude numbers from your random number generation as simply looping until you find a new number isn't a good way of doing it.

Maybe have a look at this: http://answers.unity3d.com/questions/452983/how-to-exclude-int-values-from-randomrange.html

I'd assume if you use the method which is shown in the link you should get rid of the while loop and simply write code that says "don't use usedNumbers"


You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .