Hello guys, I suck at coding and Im trying to a simple dropdown using textmesh pro, no matter where I put "Debug.LogError(“Panel scaling value " + glazingWidth);” in the following code I get an error. In update or in the method itself… it lock the game as soon as I use the dropdown or before anything happens.
I nested the script under the dropdown itself, maybe that the problem ? Should I make 1 script for the whole interface instead of one script per button/dropdown/writing field like I begun to do here ?
picture : Imgur: The magic of the Internet
Code is bellow, all I want to do is a simple dropdown, I dont know if its the correct way to do it, but if you have a much simpler way that would be great.
Thanks for the help.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using TMPro;
public class Script_UI_DD_GlazingWidth : MonoBehaviour
{
public TMP_Dropdown dropdown;
public float uIDdGlazingWidth;
public List<TMP_Dropdown.OptionData> optionDataList = new List<TMP_Dropdown.OptionData>();
private List<float> values = new List<float>();
private void Awake()
{
}
// Start is called before the first frame update
void Start()
{
setDdValue();
}
// Update is called once per frame
void Update()
{
}
void setDdValue()
{
// Create and customize the option data for each option in the dropdown
TMP_Dropdown.OptionData optionData1 = new TMP_Dropdown.OptionData("0.6 mm");
TMP_Dropdown.OptionData optionData2 = new TMP_Dropdown.OptionData("0.8 mm");
TMP_Dropdown.OptionData optionData3 = new TMP_Dropdown.OptionData("1.0 mm");
TMP_Dropdown.OptionData optionData4 = new TMP_Dropdown.OptionData("1.2 mm");
// Assign float values to each option
float value1 = 0.6f;
float value2 = 0.8f;
float value3 = 1.0f;
float value4 = 1.2f;
// Add the option data and float values to the respective lists
optionDataList.Add(optionData1);
optionDataList.Add(optionData2);
optionDataList.Add(optionData3);
optionDataList.Add(optionData4);
// ... add more option data as needed
values.Add(value1);
values.Add(value2);
values.Add(value3);
values.Add(value4);
// Assign the option data list to the dropdown
dropdown.options = optionDataList;
// Subscribe to the dropdown's OnValueChanged event and pass the OnDropdownValueChanged method as the listener
dropdown.onValueChanged.AddListener(OnDdValueChanged);
// Set the initial value of the float variable based on the first option
uIDdGlazingWidth = values[0];
}
void OnDdValueChanged(int value)
{
if (value >= 0 && value < values.Count)
{
uIDdGlazingWidth = values[value];
}
}
}