Search Unity

Dropdown options copying into other scenes

Discussion in 'Scripting' started by ilyaz_khanMohd, Feb 25, 2021.

  1. ilyaz_khanMohd

    ilyaz_khanMohd

    Joined:
    Jun 9, 2020
    Posts:
    3
    Hi, I am working on dynamically generating dropdown list options, and based on the option value and with some conditions other scenes should load. (by clicking on the dropdown value new scene should load). I have attached the all required scripts to a dropdown game object in all the scenes and the dynamic dropdown options are working for starting scene but when a new scene is loaded the dropdown of new is empty, the values of previous scense dropdown are not loading. Below is the script. Can anyone please give me a hint or a sample script to transfer the dropdown values from one scene to another. Thanks



    public class SceneLoad : MonoBehaviour
    {

    Dropdown m_Dropdown;
    private ParseHandler parsehand;
    int Scene_num;

    public void AddDropdown()
    {

    m_Dropdown = GetComponent<Dropdown>();
    m_Dropdown.ClearOptions();
    m_Dropdown.options.Add(new Dropdown.OptionData("--Starting Page--"));
    parsehand = GameObject.FindObjectOfType(typeof(ParseHandler)) as ParseHandler;
    foreach (string threat in parsehand.GetList())
    {

    if (threat == "TMP")
    {
    m_Dropdown.options.Add(new Dropdown.OptionData("Vehicle on highway changing the lane"));

    }
    if (threat == "DOS")
    {
    m_Dropdown.options.Add(new Dropdown.OptionData("Vehicle on highway met with an accident"));
    }
    if (threat == "HJK")
    {
    m_Dropdown.options.Add(new Dropdown.OptionData("Pedestrians behind the vehicle While unparking"));

    }


    }

    m_Dropdown.onValueChanged.AddListener(delegate { callingthescenes(m_Dropdown); });


    }
    private void callingthescenes(Dropdown mydropdown)
    {
    select_scene(mydropdown.options[mydropdown.value].text);

    }


    public void select_scene(string scenetext)
    {


    if (scenetext == "Pedestrians behind the vehicle While unparking")
    {

    SceneManager.LoadScene(1);

    Scene_num = 1;

    }
    if (scenetext == "Vehicle on highway changing the lane")
    {

    SceneManager.LoadScene(2);

    Scene_num = 2;

    }
    }
    }
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,735
    If you post a code snippet, ALWAYS USE CODE TAGS:

    How to use code tags: https://forum.unity.com/threads/using-code-tags-properly.143875/

    How to report your problem productively in the Unity3D forums:

    http://plbm.com/?p=220

    How to understand compiler and other errors and even fix them yourself:

    https://forum.unity.com/threads/ass...3-syntax-error-expected.1039702/#post-6730855

    Generally you get a reference to where you want to put data, and you assign it.
     
  3. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    If you're trying to transfer the player chosen value of the drop down, after they make their selection, save it to somewhere which will persist between scenes. Such as to a static variable on the same script. When your new scene loads and Start runs, make the selected value of the drop down be the value from the static variable if it is set to anything.

    If what you instead mean, is you want to save basically the entire contents of the drop down menu, I'd instead just make the drop down menu itself persist between scenes. Either via DontDestroyOnLoad, or as a separate scene you don't unload (second option here requires learning additive scene loading/unloading).
     
  4. ilyaz_khanMohd

    ilyaz_khanMohd

    Joined:
    Jun 9, 2020
    Posts:
    3
    Hi, Thanks for the info