How to execute code with a delay? In this tutorial we will take a look at how we can delay script or functions in Unity.


🧡 Subscribe to Oxmond Tutorials. Stay tuned!
https://bit.ly/SubscribeOxmondTutorials

✅ Free Assets from the Unity Assets Store:
https://assetstore.unity.com/lists/top-free-packages-13201?aid=1100l4p9k

😷👕 Need a face mask / developer T-shirt? Drop by our shop and get a 20% DISCOUNT on your first purchase by using the discount code OXMONDSALE. Click here:
https://shop.oxmond.com/discount/OXMONDSALE

Some links might be affiliate links. Any support is truly appreciated so we can keep on making high quality content

 

The Button script:

/*
   .-------.                             .--.    .-------.     .--.            .--.     .--.        
   |       |--.--.--------.-----.-----.--|  |    |_     _|--.--|  |_.-----.----|__|---.-|  |-----.
   |   -   |_   _|        |  _  |     |  _  |      |   | |  |  |   _|  _  |   _|  |  _  |  |__ --|
   |_______|__.__|__|__|__|_____|__|__|_____|      |___| |_____|____|_____|__| |__|___._|__|_____|
   © 2018-2020 OXMOND / www.oxmond.com 
*/

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Button : MonoBehaviour
{

    public GameObject textBtnIsClicked;
    public GameObject textDelayedAction;

    private void OnMouseUp()
    {
        textBtnIsClicked.SetActive(true);
        Invoke("Action", 2.0f);
    }

    private void Action()
    {
        textDelayedAction.SetActive(true);
    }

}