Random actions time based

Hi, i have this script that plays random animations on my character, works well the problem is that is frame based. The question is how would you make it so it would be time based? Also do i need to move the var random : int = Random.Range(1,5) to a start func.? It keeps giving me the error that needs to be moved to a start or awake.

var count : float = 0;
var switchSpeed: float = 1000;
var rangeDice: float = 5;
var random : int = Random.Range(1,5);

function Update()
{
    count++;
    if(count >= switchSpeed)
    {
        count = 0;
        CreatePlane();
        random = Random.Range(1,rangeDice);
    }
}

function CreatePlane()
{
    switch(random)
    {
        case 4:
        // Plays animation 4
        break;
        case 3:
        // Plays animation 3
        break;
        case 2:
        // Plays animation 2
        break;
        case 1:
        // Plays animation 1
        break;
    }
}

// Changed switchSpeed to 1, meaning one 1 second.
var switchSpeed : float = 1; 
var rangeDice : float = 5;

// When the next animation should play.
private var nextTime : float = -1;

function Update()
{
    if(Time.time >= nextTime)
    {
        nextTime = Time.time + switchSpeed;
        PlayAnimation(Random.Range(1, rangeDice));
    }
}

// Changed name to PlayAnimation to better reflect its purpose.
function PlayAnimation(animationId : int)
{
    switch (animationId)
    {
        case 4:
        // Plays animation 4
        break;
        case 3:
        // Plays animation 3
        break;
        case 2:
        // Plays animation 2
        break;
        case 1:
        // Plays animation 1
        break;
    }
}

Note that you can probably condense this code into

var switchSpeed : float = 1; 
var rangeDice : float = 5;

InvokeRepeating("PlayRandomAnimation", float.Epsilon, switchSpeed);

function PlayRandomAnimation()
{
    PlayAnimation(Random.Range(1, rangeDice));
}

function PlayAnimation(animationId : int)
{
    switch (animationId)
    {
        // ... omitted redundant code
    }
}

and I guess you could simply use an array with legal clips to further simplify this to

var switchSpeed : float = 1; 
var animations : String[];

InvokeRepeating("PlayRandomAnimation", float.Epsilon, switchSpeed);

function PlayRandomAnimation()
{
    var index = Random.Range(0, animations.Length);      
    animation.Play(animations[index]);
}

Use a coroutine. Its better for performance than calling Update every frame.

var spawnTime : float;
var rangeDice : int = 5;

function Start () {
     while(true) {
          CreatePlane(Random.Range(1, rangeDice));
          yield WaitForSeconds(spawnTime);
          //Wait x seconds.
     }
}

CreatePlane (value : int) {
     //Do your stuff here;
}


or even easier, just use `InvokeRepeating()`

var spawnTime : float;
var rangeDice : int = 5;

function Start () {
     InvokeRepeating("CreatePlane", spawnTime, spawnTime);
     //InvokeRepeating("Method", delayTime, repeatTime);  

}

CreatePlane () {
     var randomValue : int = Random.Range(1, rangeDice);
     //Do your stuff here;
}