Search Unity

Resolved Previously selected dropdown item for one object shown for other object

Discussion in 'Scripting' started by Para00100, Jun 5, 2022.

  1. Para00100

    Para00100

    Joined:
    Apr 22, 2022
    Posts:
    48
    Hi all,

    first of all I want to say that I am very new to Unity and have just some basic knowledge in programing. So pls have mercy with me if I ask stupid question :-/

    Ok my problem:
    I have two cubes in my scene and 2 buttons add label and delete label.
    By clicking on add label, the user will be able to enter labels for a gameobject (cube). The gameobjects for which labels are entered are saved in a list. And per gameobject in the list another list stored which contains the respective labels entered for the gameobject.

    By clicking on the delete label button, a panel opens which contains a dropdown listing there all labels entered for the corresponding gameobject. Per default the first label of the gameobject is selected. This all is realised by the attached script.
    However, there is a small bug, which i cannot figure out how to solve - maybe someone can help.

    If I enter some labels for the first object, everything is fine. But for the second object, the previously selected entry from the other object is shown in the dropdown as directly visible but its not selected.

    E.g.:
    For gameobject cube1 I enter the labels "l1", "l2" and "l3".
    When clicking the "Delete label" button, the displayed dropdown contains all these 3 values, where l1 is selected per default and shown directly in the dropdown as visible.

    If I now enter some labels for another object "Cube2", such as "label1" and "label2" and then clicking the "Delete label" button, the displayed dropdown has "label1" (per default selected) and "label2" but has the selected entry of cube1 (lets say l3) directly visible in the dropdown. It only disappears once I select another label in the dropdown.

    Can someone say pls, where is my mistake in the code. Would be soo thankful for that.

    Code:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5. using System;
    6. using UnityEditor;
    7.  
    8. public class DropDownHandler : MonoBehaviour
    9. {
    10.     //[SerializeField]
    11.     //public List<GameObjectLabels> gol;
    12.     private LabelHandler labelHandler;
    13.     [SerializeField]
    14.     private GameObject current_gameObject;
    15.     [SerializeField]
    16.     private List<string> lbls;
    17.     [SerializeField]
    18.  
    19.     void Start()
    20.     {
    21.         labelHandler = GameObject.Find("Labeling").GetComponent<LabelHandler>();
    22.         //gol = labelHandler.gol;
    23.         current_gameObject = labelHandler.selectedGameObject;
    24.  
    25.         FillDropDownContent(current_gameObject);
    26.        
    27.         /* old part with fixed string list - just staying here for security purpose and until all tests are done. Afterwards to be deleted!!!
    28.         var dropdown = transform.GetComponent<Dropdown>();
    29.         dropdown.options.Clear();
    30.  
    31.         //This part must be replaced by retrieving the stored label list of the corresponding gameobject later.
    32.         List<string> items = new List<string>();
    33.         items.Add("Label 1");
    34.         items.Add("Label 2");
    35.         items.Add("Label 3");
    36.         items.Add("Label 4");
    37.  
    38.         //Fill dropdown with items
    39.         foreach (var item in items)
    40.         {
    41.             dropdown.options.Add(new Dropdown.OptionData() { text = item });
    42.         }
    43.         DropdownItemSelected(dropdown);
    44.  
    45.         dropdown.onValueChanged.AddListener(delegate { DropdownItemSelected(dropdown); });
    46.         */
    47.     }
    48.     public void FillDropDownContent(GameObject go)
    49.     {
    50.         var dropdown = transform.GetComponent<Dropdown>();
    51.         dropdown.options.Clear();
    52.  
    53.         GameObjectLabels gl = labelHandler.findGameObjectLabels_by_GameObject(go);
    54.         if (gl != null)
    55.         {
    56.             lbls = gl.labels;
    57.  
    58.             //Fill dropdown with items
    59.             foreach (string item in lbls)
    60.             {
    61.                 dropdown.options.Add(new Dropdown.OptionData() { text = item });
    62.             }
    63.             DropdownItemSelected(dropdown);
    64.  
    65.             dropdown.onValueChanged.AddListener(delegate { DropdownItemSelected(dropdown); });
    66.         }
    67.     }
    68.  
    69.  
    70.     void DropdownItemSelected(Dropdown dropdown)
    71.     {
    72.         int index = dropdown.value;
    73.        
    74.         //This is just for testing purpose
    75.         Debug.Log("Selected label for deletion is the following: " + dropdown.options[index].text);
    76.     }
    77.      
    78.         // Update is called once per frame
    79.         void Update()
    80.     {
    81.         labelHandler = GameObject.Find("Labeling").GetComponent<LabelHandler>();
    82.         //gol = labelHandler.gol;
    83.         current_gameObject = labelHandler.selectedGameObject;
    84.  
    85.         FillDropDownContent(current_gameObject);
    86.     }
    87. }
    88.  
     
  2. Para00100

    Para00100

    Joined:
    Apr 22, 2022
    Posts:
    48
    puhh - was able to solve problem by myself with dropdown.RefreshShownValue(); :)