Calling random functions

I am trying to create a spawning script for my game. I have 3 spawning points, and I am choosing one of these points by random.
I also have 10 functions in another script attached to the spawning points. These functions each create a different enemy.
The way I am currently doing it is:

var spawnPoints: GameObject[];
    private var randomSpawn:GameObject;
    
    function Update ()
    {
        randomSpawn = spawnPoints[Random.Range (0,3)];
    }
    
    function Level1()
    {
    	randomSpawn.gameObject.GetComponent(spawnObjects).SpawnEnemy1();
    	randomSpawn = spawnPoints[Random.Range (0,3)];
    	randomSpawn.gameObject.GetComponent(spawnObjects).SpawnEnemy2();
    	randomSpawn = spawnPoints[Random.Range (0,3)];

    }

To sum it up, I have an array that has 3 spawn point objects in it. They are chosen randomly and then saved to randomSpawn. This is repeated in the update loop, and also in the Level1 function so that it is randomised again, before spawning the enemy two.

Somehow, I need the random spawn to also choose a random function out of these 10 functions.
These 10 functions are not in the same script.

All answers were fantastic and I managed to solve my problem with the help of all three of you :slight_smile:

Although Eric’s answer suited best for my problem.
Thanks!

Are the SpawnEnemy functions doing a great deal more than creating the enemy? If not, it’d probably be easier to set up the functions to take in a parameter (the enemy type) and pass those in.

ie.

randomSpawn = spawnPoints[Random.Range (0,3)];
enemyType = enemyTypes[Random.Range(0, 10)];
randomSpawn.gameObject.GetComponent(spawnObjects).SpawnEnemy(enemyType);

It depends on what else these SpawnEnemy functions are doing, but this would be cleaner.

Not sure I fully understand the question, but can you pass have one function that accepts a int to create different enemies?

//in the script attached to the spawn points

function SpawnEnemy(n:int)
{
    switch(n)
    {
        case 1:
            //spawn enemy 1
            //either put code here or call another function
            break;
        case 2:
            //spawn enemy 2
            break;
        case 3:
            //spawn enemy 3
            break;

        //...and so on to case 10

        default:
            //do something sensible
    }
}

//in the level script
randomSpawn.gameObject.GetComponent(spawnObjects).SpawnEnemy(Random.Range (1,10));

The literal answer to your question is to use an array of delegates:

function Start () {
    var functions = [Foo, Bar];
	functions[Random.Range(0, functions.Length)]();
}

function Foo () {
	Debug.Log ("foo");
}

function Bar () {
	Debug.Log ("bar");
}

However, you shouldn’t actually do this, since you really should not have separate functions in the first place for this sort of thing. Make one function that does spawning, and use an array of different enemy prefabs so you can choose them randomly in that function.

(Also don’t hard-code things like “spawnPoints[Random.Range (0,3)];”, instead use the length: “spawnPoints[Random.Range (0, spawnPoints.Length)];”. That way, if/when it changes, the code still works without having to remember to fix it.)