Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Attempting to make program wait before loading a new scene.

Discussion in '2D' started by Jonastrek, Sep 13, 2017.

  1. Jonastrek

    Jonastrek

    Joined:
    Sep 13, 2017
    Posts:
    3
    Hello.
    We are a small group at a school having to make a quick prototype of what is basically a menu, and so I've created this code.
    However, since it immediately loads the next scene, the Audio that is supposed to play never gets the chance to.

    Here is my code:

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using UnityEngine.SceneManagement;
    public class StartButton : MonoBehaviour {
    public float TimeBetweenClicks = 0.3333f;
    private float timestamp;

    IEnumerator BeforeLoading(){
    yield return new WaitForSeconds(0.3f);
    }

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

    }
    void OnMouseOver () {
    if (Time.time >= timestamp && (Input.GetKeyDown (KeyCode.Mouse0)) ){
    this.GetComponent<AudioSource>().Play();
    print ("You started the game! Welcome!");
    BeforeLoading();
    SceneManager.LoadScene("HousePlan");
    }
    }
    }

    the "If" statement with the timestamp is simply to keep the audio from playing a million time when the mouse button is held down.
    I asked one of my teachers, but he had no time to look at it properly, and told me to write it like this before running off.
     
  2. byteasc

    byteasc

    Joined:
    Dec 23, 2016
    Posts:
    1
    You're almost there, but your BeforeLoading actually should be handled via StartCoroutine.

    Code (CSharp):
    1. IEnumerator Loading() {
    2.      yield return new WaitForSeconds(0.3f);
    3.      SceneManager.LoadScene("HousePlan");
    4. }
    5.  
    6. void OnMouseOver() {
    7.     if (Time.time >= timestamp && (Input.GetKeyDown(KeyCode.Mouse0))) {
    8.          this.GetComponent<AudioSource>().Play();
    9.          print("You started the game! Welcome!");
    10.          StartCoroutine(Loading);
    11.    }
    12. }
    I'm not at my desk so not 100% I have it all correct, but that should be in the right direction.
     
    Deleted User likes this.
  3. Jonastrek

    Jonastrek

    Joined:
    Sep 13, 2017
    Posts:
    3
    Thanks you for the answer byeasc!
    I ended up simply changing the "onMouseOver" to "onMouseDown", which meant I could drop the timestamps.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.SceneManagement;
    5.  
    6. public class StartButton : MonoBehaviour {
    7.  
    8.     private float timer = 0f;
    9.     public bool X = false;
    10.     // Update is called once per frame
    11.     void Update () {
    12.         if (X == true){
    13.             timer += Time.deltaTime;
    14.         }
    15.         if (timer >= 0.3f){
    16.             SceneManager.LoadScene("HousePlan");
    17.         }
    18.  
    19.         if (timer >= 1f){
    20.             X = false;
    21.         }
    22.  
    23.     }
    24.  
    25.         void OnMouseDown(){
    26.             X = true;
    27.             this.GetComponent<AudioSource>().Play();
    28.             print ("You started the game! Welcome!");
    29.  
    30.     }
    31. }
    By making a bool, I could make it so a timer counted down. Further, I could have made the 0.3f:
    Code (CSharp):
    1. if (timer >= 0.3f){
    2.             SceneManager.LoadScene("HousePlan");
    3.         }
    4.  
    here its own public float, so that it could be easily changed.
    Of course, by doing this you have to be careful to stop the timer by turning the bool X back to false, or else it'd go on forever. I don't think it matters in my case, as I am loading a new scene, though.