How would I restart a scene by clicking on something?
So in my game I want to be able to restart by clicking twice within one second on a certain box, if they only click once than nothing happens, but when I try to implement this I get all sorts of errors about where to put what. Can someone explain what I am to do?
Here is the code I have so far
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.SceneManagement;
public class Restart_Level : MonoBehaviour {
public float timeleft;
float timeLeft = 1.0f;
private bool Confirm;
public int Save;
private void OnTriggerEnter(Collider other)
{
{
Confirm = true;
Save = Save + 1;
yield return new WaitForSeconds(1);
if (Confirm == true)
{
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
}
if (Save == 1)
{
Confirm = false;
Save = 0;
}
}
}
}
Answer by $$anonymous$$ · Feb 19, 2017 at 09:11 AM
I'm not sure if you mean a physical 3d box or a UI box, but for a 3d box, you could do something like
public class ReloadScene : MonoBehaviour {
float lastClickTime = 0f;
void OnMouseDown (){
// If the time passed since this object was last click is less than 1 ...
if(Time.time - lastClickTime < 1 && Time.time > 1){
// ... reload the scene
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
}
lastClickTime = Time.time;
}
}
This may also work for a UI element, but I'm not sure.
Also, note that this won't work properly within the 1st second of playing the game (Nothing will happen when you double click).
Hope this helped!
Your answer
![](https://koobas.hobune.stream/wayback/20220612111431im_/https://answers.unity.com/themes/thub/images/avi.jpg)