wait random time

hello

I have this script

using UnityEngine;
using System.Collections;

public class helikopterscript : MonoBehaviour {

    public float speed = 5;

    RaycastHit hit;

    public GameObject explosie;

    public gamemanager Gamenager;

    string positionsString = "";

    public bool magbombdroppen = false;



   
    // Use this for initialization
    void Start () {
        Gamenager = Camera.main.GetComponent<gamemanager>();
    }
   
    // Update is called once per frame
    void Update () {
        transform.Translate(Vector3.forward * speed * Time.deltaTime);

        Debug.DrawRay(transform.position, -transform.up * 10, Color.green);

        if (Physics.Raycast(transform.position, -transform.up, out hit, 10))
        {
            if (hit.collider.gameObject.tag == "vloer")
            {
                if(magbombdroppen == true)
                {
                    if (Random.value > 0.5f)
                    {
                        //random time here
                    }
                }
            }
        }
   
    }

    void ApplyDamage(float damage)
    {
        Gamenager.targetshit += 1;
        Gamenager.AddPositionToPositionString(transform);
        Instantiate(explosie, new Vector3(transform.position.x, transform.position.y, transform.position.z), Quaternion.identity);
        Destroy(gameObject);
    }

    void OnTriggerEnter(Collider col)
    {
        if (col.gameObject.tag == "scoreeraf")
        {
            Gamenager.targetshit -= 1;
            Destroy(gameObject);
        }

        if (col.gameObject.tag == "magbomdroppe")
        {
            magbombdroppen = true;
        }
    }
}

what i wanna do is when the random.value > 0.5 it needs to have a random time of like 1 to 5 seconds
But how do i do that?
Hope someone can help

You could use invoke and give it a random time. Something like;

Invoke(somefunction, Random.Range(1.0f, 5.0f));

Coroutines and yield: http://unity3d.com/learn/tutorials/modules/intermediate/scripting/coroutines
You could write a coroutine with

yield return new WaitForSeconds(seconds);

For seconds you can use Unity - Scripting API: Random.Range

seconds = Random.Range(1.0f, 5.0f);

hi

but i cant figure out
how to use invoke in this script? i read the reference page and i know what it does right now
but how do i call update method that has the raycast and stuff in it since the update is already updating

EDIT
i have this now

Invoke(bombdrop, Random.Range(1.0f, 5.0f));

but i get these 2 errors
Error 1 The best overloaded method match for ‘UnityEngine.MonoBehaviour.Invoke(string, float)’ has some invalid arguments
Error 2 Argument 1: cannot convert from ‘method group’ to ‘string’

Take a look at your errors

Error 1 The best overloaded method match for ‘UnityEngine.MonoBehaviour.Invoke(string, float)’ has some invalid arguments
Error 2 Argument 1: cannot convert from ‘method group’ to ‘string

The invoke function needs to be a string.

Invoke("bombdrop", Random.Range(1.0f, 5.0f));
1 Like