When clicked do something

Hi everyone

I want to say I have a problem and I don’t know how to fix it.

I created a button and when it’s clicked, I want it to wait 1 second before doing other stuff it should. When button is clicking, it’s not waiting.

This is the script:

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

public class ClickAndWait : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {
       
    }

    // Update is called once per frame
    void Update()
    {
       
    }

    private void OnMouseDown()
    {
        System.Threading.Thread.Sleep(1000);
    }
}

Please note it’s my first time experience with Unity and C#. To this, I’ve added ‘Add to list Click On’ thing and there is script, but I don’t know what to do more to make this work. It’s my second day fixing it and still nothing.

Thanks :slight_smile:6668842--763285--clicknwait.PNG

First of all, you do not want to sleep the Unity thread. I’m not sure if that even works, but if it does, you’d simply freeze your program for the given amount of time. Instead, you probably want to start a Coroutine which waits some time, and afterwards executes what you want to happen after a certain amount of time has passed: vuforia - How to Achieve Thread.Sleep() Functionality in Unity Script? - Stack Overflow

Secondly:

  • A method that is to be selected for something like OnClick must be public (not private)
  • You are using an existant name “OnMouseDown”, which should be avoided
  • To actually call that method (after the above is fixed), select it in the dropdown that currently displays MonoScript.name. Your public method “SomeName” should be displayed there.