0
\$\begingroup\$

When the object gets destroyed in my game, I want a touch to continue function after a delay. This is what I tried.

this is the SceneManager class which is attached to the Main Camera

 public bool ttc;
 public void NextLevel()
{
    StartCoroutine(Delay());
}
IEnumerator Delay()
{

    yield return new WaitForSeconds(2);
    print("touch to continue");
    ttc = true;}

My Object class:

   void OnDestroy()
  {
   Camera.main.GetComponent<SceneManager>().NextLevel();
    if (Camera.main.GetComponent<SceneManager>().ttc == true)
    {
        if (Input.GetKeyDown(KeyCode.V))
        {
            Application.LoadLevel("TheGame2");
        }
    }
}

I also tried this:

IEnumerator Delay()
{
    yield return new WaitForSeconds(2);
    print("touch to continue");
    if (Input.GetKeyDown(KeyCode.V))
        {
            Application.LoadLevel("TheGame2");
        }
    }

side note: I will change the 'KeyCode.V' to a mouse button later

\$\endgroup\$

2 Answers 2

0
\$\begingroup\$

Do this:

IEnumerator Delay()
{
    yield return new WaitForSeconds(2);
    print("touch to continue");
    var pressed = false;
    while(!pressed){
        if (Input.GetKeyDown(KeyCode.V))
        {
            pressed = true;
        }
        yield return null;
    }
    Application.LoadLevel("TheGame2");
}
\$\endgroup\$
0
\$\begingroup\$

Since you stated you will add a mouse button, I'll add that code here as well.

You would need to attach this to your scene manager

public bool IsDestroyed = false;
public bool ActivateGUI = false;

IEnumerator Delay()
{
    yield return new WaitForSeconds(2);
    ActivateGUI = true;
}

void OnGUI() //Button related stuff
{
    if (ActivateGUI)
        if (GUI.Button(new Rect(Screen.width - 10 - 100, Screen.height - 10 - 50, 100, 50), "Press Here"))
            Debug.Log("This repalces the press V code");
}


void Update()
{
    if (IsDestroyed)
    {
        StartCoroutine(Delay());
    }
}

The Update funtion is checking every frame if you object is detroyed. The OnGUI stuff is what draw the button.

Your OnDestroy funtion shoul be something similar to this

void OnDestroy()
{
    GameObject go = GameObject.Find("GameManager");
    GameManager gm = go.GetComponent<GameManager>();
    gm.IsDestroyed = true;

    Destroy(gameObject);
    Debug.Log("Object destroid");
}

I have read on the Unity forums that the GameObject.Find it is a really slow funtion, so caution is advised.

Hope this helps.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .