Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.

Question Getting text string from dropdown options

Discussion in 'Visual Scripting' started by Canley, Oct 7, 2022.

  1. Canley

    Canley

    Joined:
    Jun 17, 2022
    Posts:
    27
    Hey guys,

    I'm working in unity 2021.3.9f1 and want to grab the text value from a TMP dropdown list, and pass it into a string variable.

    I want to do that so I can allow users to set their own dropdown list items. I also use the list items to set section headings in a panel. (Plus my functioning graph isn't exactly streamlined.)

    I've found a c# code snippet, but I can't find the right nodes to convert it to visual scripting. I"m avoiding c# as much as possible.

    the code is: Debug.Log(Dropdown.options[Dropdown.value].text);

    I've tried a variety of nodes, as you'll see from the screenshots. (Pic one is all the nodes - pic 2 is zoomed in to show detail.) The transform node I'm using is to try and grab the string from the dropdown label child object.

    Can anyone point me in the right direction?

    Thanks heaps for the help,
    Canley

    dropdown script full.png dropdown script detail.png
     
  2. REDACT3D_

    REDACT3D_

    Joined:
    Nov 8, 2020
    Posts:
    222
    TMP_Dropdown
    Get Caption Text ---> TMP_Text Get Text
    outputs the string lol
    Capture.PNG


    but looks like you're getting a null component
     
  3. Canley

    Canley

    Joined:
    Jun 17, 2022
    Posts:
    27
    Thanks that looks great, and the null might be because I was trying to get it from the dropdown label rather than the parent.

    I can't find the TMP_Text nodes though. Guessing I'll need to add them in preferences, but no luck trying things as yet.

    Any idea what I need to add? Is it a type option?

    Thanks heaps :)
     
  4. REDACT3D_

    REDACT3D_

    Joined:
    Nov 8, 2020
    Posts:
    222
    Make sure you have the TMP assembly in the Visual Scripting Project Settings window

    not a type but the assembly
    Capture.PNG
     
  5. Canley

    Canley

    Joined:
    Jun 17, 2022
    Posts:
    27
    Thanks Redact.

    I had Unity.TextMeshPro already, but not .Editor.

    So I've added that, regenerated, restarted the project, but still no "tmp_text" nodes.

    Gonna try in an empty project and see if that helps.
     
  6. Canley

    Canley

    Joined:
    Jun 17, 2022
    Posts:
    27
    no joy with that attempt :(
     
  7. REDACT3D_

    REDACT3D_

    Joined:
    Nov 8, 2020
    Posts:
    222
    just type "TMP get text" into the fuzzy finder.
    drag every single get text you see onto the graph and try to match up the icons

    the new TMP is not the same as old TMP
    bunch of versions that all spamm it up

    you can try to add tmp text as a type and regenerate too


    do you have the TMP dropdown?
     
  8. Canley

    Canley

    Joined:
    Jun 17, 2022
    Posts:
    27
    Great thanks I'll try all that. I've got the TMP dropdown.
     
  9. Canley

    Canley

    Joined:
    Jun 17, 2022
    Posts:
    27
    It's working!

    Thanks for the guidance I really appreciate it :)

    working dropdown label.png

    So this might not be the *best* solution, but what I've done is add a tag to the label child object of the dropdown, find that object, and use text mesh pro ugui to pull the text from it.

    One error in what I'd tried with earlier exploring was to look for text mesh pro nodes rather than text mesh pro ugui.

    Just trying to match up the icons helped a lot.

    Cheers,
    Canley
     
    REDACT3D_ likes this.
  10. PanthenEye

    PanthenEye

    Joined:
    Oct 14, 2013
    Posts:
    1,033
    You'll quickly run into performance issues with Find methods in OnUpdate. Any kind of Find is slow already, performing that every frame quickly compounds the issue. It would be better to cache the object you're looking for in an Object variable manually, or use Find in OnEnable or OnStart.

    Finding the object by loose string names also keeps your project architecture fragile - if you rename the object later or remove it completely, the logic breaks. Variable use would aid in keeping the project manageable long term.
     
    jlbaker2779 likes this.
  11. Canley

    Canley

    Joined:
    Jun 17, 2022
    Posts:
    27
    Thank you PanthenEye - I'll integrate that. I probs use OnUpdate way too much.
     
  12. REDACT3D_

    REDACT3D_

    Joined:
    Nov 8, 2020
    Posts:
    222
    WooT

    If it wasn't for the fuzzy finder and expose nodes i'd have never found it lol

    look at the size of this expose node! rofl
    Capture.PNG Capture1.PNG
     
  13. Canley

    Canley

    Joined:
    Jun 17, 2022
    Posts:
    27
    Crazy!

    I'll have to try fix my tmp_text problem somehow - lots of nodes seem to output it.
     
  14. Canley

    Canley

    Joined:
    Jun 17, 2022
    Posts:
    27
    Ok well I still haven't been able to add TMP_Text nodes, but I added Text Mesh Pro UGUI as a type in settings, then was able to pull a string directly from the dropdown caption text as in the pic:

    working dropdown text using tmp ugui.png

    That's simple and doesn't require either finding the label child object or giving it a variable.

    Thanks again for the help :)
     

    Attached Files:

    REDACT3D_ likes this.