Reputation: 81
i'm trying to make a code that changes the text of the UI by a string variable that's in another function but i get this
error: Assets/Scripts/ChangeQuestion1.cs(15,26): error CS0029: Cannot implicitly convert type
UnityEngine.UI.Text' to
string
What is the best way to convert a UI text to a string? I'm sorry if this question sound noobish. Here's the code:
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 () {
}
}
UPDATE:
I already solved the problem, it was more complicated than that. The variable i was trying to print in canvas was from a script to connect with a database and i forgot to call some functions too.
Upvotes: 0
Views: 5743
Reputation: 380
Question1.text
is of type string
, and you're trying to set its value to Question1
, whose type is UnityEngine.UI.Text
. You can only set its value to be a string
, so it should be:
Question1.text = yourText;
or
Question1.text = "Your text goes here";
This is how it would look like in your code:
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 () {
}
}
Upvotes: 3