Search Unity

How would I add a scene to an existing string on button press?

Discussion in 'Scripting' started by GIitch, Feb 12, 2019.

  1. GIitch

    GIitch

    Joined:
    Aug 1, 2013
    Posts:
    24
    I'm working on a script for asynchronous level loading.

    To explain the intended functionality, this script lets the player select levels from a physical menu within the world space. When a button is pressed, the script partially loads the corresponding scene without unloading the current scene and won't complete this process until the player collides with a designated trigger object.

    I think I've got the basic components working, but I've no clue how to build this last part. On button press, the button should add the corresponding scene to the string 'SelectedScene'. This would allow for multiple scene buttons.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.SceneManagement;
    5. using UnityEngine.UI;
    6. using TMPro;
    7.  
    8. public class LevelSelect : MonoBehaviour
    9. {
    10.     public TMP_Text m_Text;
    11.     public Button m_Button;
    12.  
    13.     void Start()
    14.     {
    15.         m_Button.onClick.AddListener(LoadButton);
    16.     }
    17.  
    18.     void LoadButton()
    19.     {
    20.         AsyncOperation asyncOperation = SceneManager.LoadSceneAsync("SelectedScene");
    21.         asyncOperation.allowSceneActivation = false;
    22.     }
    23.  
    24.     public void OnTriggerEnter(Collider other)
    25.     {
    26.         if (CompareTag("Player") == true);
    27.         {
    28.             StartCoroutine(LoadScene());
    29.         }
    30.     }
    31.  
    32.     IEnumerator LoadScene()
    33.     {
    34.         yield return null;
    35.  
    36.         AsyncOperation asyncOperation = SceneManager.LoadSceneAsync("SelectedScene");
    37.        
    38.         asyncOperation.allowSceneActivation = true;
    39.         Debug.Log("Progress :" + asyncOperation.progress);
    40.  
    41.         while (!asyncOperation.isDone)
    42.         {
    43.             m_Text.text = "Loading progress: " + (asyncOperation.progress * 100) + "%";
    44.  
    45.             if (asyncOperation.progress >= 0.9f)
    46.             {
    47.                 m_Text.text = "Level loaded";
    48.                 asyncOperation.allowSceneActivation = true;
    49.             }
    50.  
    51.             yield return null;
    52.         }
    53.     }
    54. }
     
  2. eses

    eses

    Joined:
    Feb 26, 2013
    Posts:
    2,637
    Hi @GIitch

    You could add a small script into your trigger object or Button.

    Make it have a string field, where you input the target level.

    Then on trigger enter, make your new script call your LevelSelect class LoadButton().

    If you want to use Button instead, call the script on button press.

    Make the LoadButton method accept a string as parameter.

    Then pass this parameter to your coroutine in similar manner.