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.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Kleine Frage zu dropdown

Discussion in 'Scripting' started by marti99x, Aug 16, 2022.

  1. marti99x

    marti99x

    Joined:
    Jul 19, 2022
    Posts:
    2
    Hallo ich habe mal eine Frage zum Thema Dropdown.

    Ich habe zwei verschiedene Dropdowns in Unity erstellt die jeweils die Zahlen 1-5 wählbar machen und möchte nun gerne diese beiden mithilfe von einem c # - script auslesen und z.b. in der Console ausgeben oder später speichern.
    Kann mir da jemand eine anregung geben wie ich das machen kann.
    Habe bisher zu dem Thema leider kaum etwas gefunden was mir weiter hilft.
    mein erster Ansatz:

    Code (CSharp):
    1. mit System;
    2. Verwenden von System.Collections;
    3. mit System.Collections.Generic;
    4. mit UnityEngine;
    5. mit UnityEngine.UI;
    6.  
    7. öffentliche Klasse TestAnzeige : MonoBehaviour
    8. {
    9. privates Dropdown-Dropdown-Dropdown1;
    10. privates Dropdown-Dropdown-Dropdown2;
    11.  
    12.     void Start()
    13.     {
    14.          dropdown1 = GetComponent<Dropdown>();
    15.          dropdown1.onValueChanged.AddListener();
    16.  
    17.         dropdown2 = GetComponent<Dropdown>();
    18.         dropdown2.onValueChanged.AddListener();
    19.         Debug.Log(dropdown1 + "Erster Wert nun folgt der zweite: " + dropdown2);
    20.     }
    21. }
     
  2. Kreshi

    Kreshi

    Joined:
    Jan 12, 2015
    Posts:
    433
    AddListener braucht eine Funktion als Input siehe Doku dazu hier: https://docs.unity3d.com/2019.1/Documentation/ScriptReference/UI.Dropdown-onValueChanged.html

    Diese Schreibweise + Info könnte für dich eventuell auch interessant sein:

    Code (CSharp):
    1.         // Debug.Log wird jedes Mal bei einem onValueChanged event ausgeführt. value == der neu gesetzte Wert des Dropdowns.
    2.         UnityEngine.UI.Dropdown dropdown = this.GetComponent<UnityEngine.UI.Dropdown>();
    3.         dropdown.onValueChanged.AddListener((value) =>
    4.         {
    5.             Debug.Log(value);
    6.         });
    7.  
    8.         // der aktuelle Wert des Dropdowns kann auf diese Weise zu jedem Zeitpunkt direkt ausgelesen werden
    9.         Debug.Log(dropdown.value);
     
  3. marti99x

    marti99x

    Joined:
    Jul 19, 2022
    Posts:
    2
    https://docs.unity3d.com/2019.1/Documentation/ScriptReference/UI.Dropdown-onValueChanged.html

    Code (CSharp):
    1. using System;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5. using UnityEngine.UI;
    6.  
    7. public class dropdownValue : MonoBehaviour
    8. {
    9.     Dropdown m_Dropdown;
    10.     public Text m_Text;
    11.  
    12.     void Start()
    13.     {
    14.         //Fetch the Dropdown GameObject
    15.         m_Dropdown = GetComponent<Dropdown>();
    16.         //Add listener for when the value of the Dropdown changes, to take action
    17.         m_Dropdown.onValueChanged.AddListener(delegate {
    18.             DropdownValueChanged(m_Dropdown);
    19.         });
    20.  
    21.         //Initialise the Text to say the first value of the Dropdown
    22.         m_Text.text = "First Value : " + m_Dropdown.value;
    23.     }
    24.  
    25.     //Ouput the new value of the Dropdown into Text
    26.     void DropdownValueChanged(Dropdown change)
    27.     {
    28.         m_Text.text = "New Value : " + change.value;
    29.     }
    30. }
    ich hab das jetzt mal mit diesem Code versucht bekomme dann aber leider:

    "NullReferenceException: Object reference not set to an instance of an object
    dropdownValue.Start () (at Assets/Scripts/dropdownValue.cs:17)"
     
  4. Kreshi

    Kreshi

    Joined:
    Jan 12, 2015
    Posts:
    433
    m_Dropdown ist null, vermutlich weil dein script auf ein gameobject drauf gelegt wurde, welches kein Dropdown Element drauf hat. GetComponent sucht nur im aktuellen GameObject nach der Komponente. Du kannst die Variable auch public machen und über den Inspektor über drag&drop zuweisen. Ich würde dir empfehlen, Tutorials zu GetComponent zu suchen :).
     
  5. orionsyndrome

    orionsyndrome

    Joined:
    May 4, 2014
    Posts:
    3,043
    omg you Germans even translate keywords in programming languages!!!
    what's next? BekommenKomponente?? :) nichtig Start()??? ;)

    SpielObjekte von Einheit!
    Love it, sounds so technical, like it might even work lol
     
    Bunny83, b3x2088 and Kreshi like this.
  6. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    3,571
    As a german myself I had a good laugh when I saw that auto translated script. To me it looks painfully odd :). Funny though that the translator translated using with "mit" in most cases and with "verwenden von" only once (which would be the more correct translation here).

    Anyways, you really should try to stick to english here.
     
    orionsyndrome likes this.
  7. Crimbobimbo

    Crimbobimbo

    Joined:
    Mar 15, 2023
    Posts:
    1
    Hallo
    Ich hoffe das ist das was du meitest.

    public void HandleInputData(int val)
    {
    Debug.Log(val + 1);
    }

    Das Script mußt du aber auf einem Gameobjekt namens Main platzieren sonst funktionirt es nicht
    und das Script dann beim OnChanged Event im Inspektor angeben. Val gibt immer die nummer in der Liste
    aus.