将UI文本转换为Unity(C#)中的字符串的最佳方法是什么?
我试图编写一个代码,通过另一个函数中的字符串变量更改UI的文本,但我得到了这个结果 错误:Assets/Scripts/ChangeQuestion1.cs(15,26):错误CS0029:无法 将类型将UI文本转换为Unity(C#)中的字符串的最佳方法是什么?,c#,visual-studio,unity3d,C#,Visual Studio,Unity3d,我试图编写一个代码,通过另一个函数中的字符串变量更改UI的文本,但我得到了这个结果 错误:Assets/Scripts/ChangeQuestion1.cs(15,26):错误CS0029:无法 将类型UnityEngine.UI.Text'隐式转换为string 将UI文本转换为字符串的最佳方式是什么? 如果这个问题听起来很愚蠢,我很抱歉。 代码如下: using System.Collections; using System.Collections.Generic; using Unity
UnityEngine.UI.Text'隐式转换为string
将UI文本转换为字符串的最佳方式是什么?
如果这个问题听起来很愚蠢,我很抱歉。
代码如下:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class ChangeQuestion1 : MonoBehaviour {
public GameObject databaseinterfaceinstance;
public Text Question1;
// Use this for initialization
void Start () {
databaseinterfaceinstance = GameObject.FindWithTag("DatabaseInterface").GetComponent<GameObject>();
Question1 = gameObject.GetComponent<Text>();
Question1.text = Question1;
}
// Update is called once per frame
void Update () {
}
}
使用系统集合;
使用System.Collections.Generic;
使用UnityEngine;
使用UnityEngine.UI;
公共阶层变化问题1:单一行为{
公共游戏对象数据库接口安装;
公共文本问题1;
//用于初始化
无效开始(){
databaseinterfaceinstance=GameObject.FindWithTag(“DatabaseInterface”).GetComponent();
问题1=gameObject.GetComponent();
问题1.文本=问题1;
}
//每帧调用一次更新
无效更新(){
}
}
更新:
我已经解决了这个问题,比那更复杂。我试图在画布中打印的变量来自一个与数据库连接的脚本,我也忘了调用一些函数。Question1.text
的类型是string
,您试图将其值设置为Question1
,其类型是UnityEngine.UI.text
。只能将其值设置为字符串
,因此应为:
Question1.text = yourText;
或
这是代码中的情况:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class ChangeQuestion1 : MonoBehaviour {
public GameObject databaseinterfaceinstance;
public Text Question1;
public string yourText = "Your text goes here";
// Use this for initialization
void Start () {
databaseinterfaceinstance = GameObject.FindWithTag("DatabaseInterface").GetComponent<GameObject>();
Question1 = gameObject.GetComponent<Text>();
Question1.text = yourText;
}
// Update is called once per frame
void Update () {
}
}
使用系统集合;
使用System.Collections.Generic;
使用UnityEngine;
使用UnityEngine.UI;
公共阶层变化问题1:单一行为{
公共游戏对象数据库接口安装;
公共文本问题1;
公共字符串yourText=“您的文本放在这里”;
//用于初始化
无效开始(){
databaseinterfaceinstance=GameObject.FindWithTag(“DatabaseInterface”).GetComponent();
问题1=gameObject.GetComponent();
问题1.文本=你的文本;
}
//每帧调用一次更新
无效更新(){
}
}
Question1.text
的类型为string
,您试图将其值设置为Question1
,其类型为UnityEngine.UI.text
。只能将其值设置为字符串
,因此应为:
Question1.text = yourText;
或
这是代码中的情况:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class ChangeQuestion1 : MonoBehaviour {
public GameObject databaseinterfaceinstance;
public Text Question1;
public string yourText = "Your text goes here";
// Use this for initialization
void Start () {
databaseinterfaceinstance = GameObject.FindWithTag("DatabaseInterface").GetComponent<GameObject>();
Question1 = gameObject.GetComponent<Text>();
Question1.text = yourText;
}
// Update is called once per frame
void Update () {
}
}
使用系统集合;
使用System.Collections.Generic;
使用UnityEngine;
使用UnityEngine.UI;
公共阶层变化问题1:单一行为{
公共游戏对象数据库接口安装;
公共文本问题1;
公共字符串yourText=“您的文本放在这里”;
//用于初始化
无效开始(){
databaseinterfaceinstance=GameObject.FindWithTag(“DatabaseInterface”).GetComponent();
问题1=gameObject.GetComponent();
问题1.文本=你的文本;
}
//每帧调用一次更新
无效更新(){
}
}
Question1.text=Question1代码>没有逻辑意义,您试图将对象的text
属性指定为对象本身GetComponent()
也没有意义..我更新了帖子,看一看尝试打印问题1<代码>Debug.Log(Question1Display.GetComponent().Question1)代码>。它打印什么吗?还有,yourText
的分配正确吗?我已经解决了这个问题,它比那更复杂。我试图在画布中打印的变量来自与数据库连接的脚本,我忘了调用一些函数代码>没有逻辑意义,您试图将对象的text
属性指定为对象本身GetComponent()
也没有意义..我更新了帖子,看一看尝试打印问题1<代码>Debug.Log(Question1Display.GetComponent().Question1)代码>。它打印什么吗?还有,yourText
的分配正确吗?我已经解决了这个问题,它比那更复杂。我试图在画布中打印的变量来自与数据库连接的脚本,我也忘了调用一些函数。GetComponent()
仍然没有意义,我试图根据另一个函数中变量的值转换ui文本。很抱歉没有说清楚。GetComponent()
仍然没有意义,因为我正在尝试根据另一个函数中变量的值转换ui文本。对不起,我不太清楚。