1

I currently work on Unity and using C# language.

By far, I need to do something (in smallest case, load another scene).

My question is, how to make this something happens after a few seconds I put the cursor on the area?

this is my code right now.

 if (_newGameButton.Contains (Event.current.mousePosition)) {   
                Application.LoadLevel (1);
                Destroy (this);
            }

I want to add delay when _newGameButton actives. Right now, when I move my cursor over _newGameButton it'll load scene 1 immediately. I have tried many ways such as using Invoke and WaitForSeconds. None works. If the mistake is how I use it, how's the right way? Thank you so much for your help.

EDIT: This question is answered and I have another question in Activate and Deactivate Invoke function.

1
  • Mikurin to make timers in Unity, just use Invoke. very easy. Commented Jun 19, 2016 at 17:35

3 Answers 3

2

To make timers and delays in Unity, simply use Invoke

void Start()
 {
 Debug.Log("hello from Start.");
 Invoke("Test", 3f);
 }

private void Test()
 {
 Debug.Log("hello from 'Test'");
 }

Also very handy is InvokeRepeating

 Invoke("Test", 5f, 0.5f);

It's that easy.

In your case

if (_newGameButton.Contains (Event.current.mousePosition))
  {
  Invoke("YourSceneName");
  }

private void ChangeScenes()
 {
 UnityEngine.SceneManagement.SceneManager.LoadScene("ScreenMain");
 }

You MUST USE the scene name. Don't forget you MUST HAVE DRAGGED THE SCENE, TO YOUR SCENE LIST. Look at "Build Settings" "Scenes in Build".


Note

That's not really how you load scenes in Unity these days. They changed the syntax. it's more like this...

    UnityEngine.SceneManagement.SceneManager.LoadScene("ScreenMain");

If you want to load asynchronously

AsyncOperation ao;
ao = UnityEngine.SceneManagement.SceneManager.LoadSceneAsync("SceneName");
while (!ao.isDone)
  {
  Debug.Log("loading " +ao.progress.ToString("f2"));
  yield return null;
  }

If you have any questions about scene loading, ask them separately as a new question. Note that you should almost certainly NOT do this "Destroy(this);".

Sign up to request clarification or add additional context in comments.

7 Comments

I use Unity 5.1.1 and the SceneManagement doesn't work. And using Invoke doesn't work either. My if is on onGUI function. Does the ChangeScenes function have to be after onGUI function or before?
Hi, are you saying this does not work: ` UnityEngine.SceneManagement .SceneManager .LoadScene("ScreenMain");` @Mikuriin
Yes, it is. But it doesn't matter, actually. The problem is _newGameButton is a rectangle not a button and the function I use to load the scene is inside onGUI function and I confused where should I put the ChangeScenes function. After or before the onGUI function.
what, you are using "ongui"? you can't - it is deprecated and doesnt work. (1) click "add canvas" (select "scale with screen size" - you almost always want that). (2) click "add button". you're done.
OK, fixed that. Now, what if I want to cancel the invoke when the cursor is out of the designated area? I try to use CancelInvoke() as else{} after if (_newGameButton.Contains), and it doesn't work as the invoke is cancelled forever because the cursor doesn't move to _newGameButton. how to set this CancelInvoke JUST if the cursor is out of the area?
|
1

Well ill help you understand something a bit bigger then just pausing cause it seems you are trying to create on OnClick event, nowadays its being used differently.

what you realy want to do is just create the SceneLoader script which can be a very general script such as the following,

using UnityEngine;
using UnityEngine.UI;
public class UIController : MonoBehaviour {

public int numOfSceneToLoad; // number of the scene from the build settings

  public void LoadSceneNumber(numOfSceneToLoad){
    UnityEngine.SceneManagement.SceneManager.LoadScene(numOfSceneToLoad);
  }

}

and then you want to attach this script to your main UI Canvas/Holderplace, go to your button and at the bottom of the inspector you will see onClick(), click on the + sign and then make the setup as follows, drag the Object you have attached this script to and put it under the "runtime" laber in that small onClick() window, then scroll and find this specific function we just created and in there you can modify the value of numOfSceneToLoad to the scene you want to load, and woilla you have a Dynamic script to load any scene you wish to.

on this note ill reffer you to some pretty amazing toturials made by unity that will teach you how to make a proper UI in unity5.

http://unity3d.com/learn/tutorials/topics/user-interface-ui/ui-button

1 Comment

it's a nice explanation.
0

From your description, it looks to me that your _newGameButton is set to inactive by default and you want it to active and user intractable after few delay.

For that you can create a script and keep reference of _newGameButton in it, something like this:

public GameObject _newGameButton;

and on an event say GameOver, implement like this:

void GameOver()
{
   Invoke("EnableNewGameButtonAfterDelay", 5f);
}


void EnableNewGameButtonAfterDelay()
{
   _newGameButton.SetActive(true);
   // now this game object is active and user can click on it.
}

In this example i demonstrated that the game has been over and after 5 second delay user have new game button set to enable. [This code is not in executable form and just to give you an idea]

Hope this helps!

1 Comment

_newGameButton is a Rectangle not a button. The case is I use Kinect and Kinect MS-SDK from Asset store, I didn't know how to click using Kinect so I use rectangle instead to occur an event. And then it occurs immediately if the cursor is moved to the rectangle. So it can't use _newGameButton.SetActive

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.