问题描述
我正在尝试从文本框中输入值,然后从第一个下拉列表和第二个下拉列表中选择值以在第二个只读文本框中显示所需的值。假设这用于转换不同类型长度的值。我把唤醒功能放在开始拉下拉默认值。所以我收到这个错误“对象引用未设置到对象 convert.Awake() 的实例”。请告诉我我怎么做以及我做错了什么?我知道这很容易,但我是新手:(
public InputField fromValue;
public InputField tovalue;
public UnityEngine.UI.Dropdown from = null;
public UnityEngine.UI.Dropdown to = null;
public UnityEngine.UI.Button convertButton = null;
// Start is called before the first frame update
void Start()
{
from = GetComponent<UnityEngine.UI.Dropdown>();
to = GetComponent<UnityEngine.UI.Dropdown>();
Awake();
}
// Update is called once per frame
void Update()
{
}
public void Awake()
{
convertbutton.onClick.AddListener(() => { CompareValues(); });
from.onValueChanged.AddListener((newValueOnLeft)
=>
{ CompareValuesWithTo(newValueOnLeft); });
to.onValueChanged.AddListener((newValueOnRight)
=>
{ CompareValuesWithFrom(newValueOnRight); });
}
public void CompareValues()
{
int rightValue = from.value;
int leftValue = to.value;
if (leftValue == rightValue)
{
tovalue.text = "Match " + "f: " + from.options[from.value].text + "t: " + to.options[to.value].text;
//Debug.Log("Match!");
}
else
{
tovalue.text = "No Match";
//Debug.Log("Not Match!");
}
}
public void CompareValuesWithTo(int newValueOnLeft)
{
int rightValue = to.value;
if (newValueOnLeft == rightValue)
{
Debug.Log("Match!");
tovalue.text = "Match " + "f: " + from.options[from.value].text + "t: " + to.options[to.value].text;
}
else
{
Debug.Log("Not Match!");
tovalue.text = "No Match";
}
}
public void CompareValuesWithFrom(int newValueOnRight)
{
int leftValue = from.value;
if (leftValue == newValueOnRight)
{
Debug.Log("Match!");
tovalue.text = "Match " + "f: " + from.options[from.value].text + "t: " + to.options[to.value].text;
}
else
{
Debug.Log("Not Match!");
tovalue.text = "No Match";
}
}
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)