- Home /
using UnityEngine.UI; 'Not necessary' although I need to use 'Text' and '.text'. Also unable to drag-and-drop the Text from my canvas into the 'text' slot.
I am confused as to why VS is telling me that UnityEngine.UI isn't necessary although I need to use aspects of it for the text in my game.
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI;
public class TextBoxManager : MonoBehaviour {
public GameObject textBox;
public Text theText;
public TextAsset textfile;
public string[] textlines;
public int currentLine;
public int endAtLine;
public PlayerBehaviour player;
// Use this for initialization
void Start()
{
player = FindObjectOfType<PlayerBehaviour>();
theText = GetComponent<Text>();
if (textfile != null)
{
textlines = (textfile.text.Split('\n'));
}
if(endAtLine == 0)
{
endAtLine = textlines.Length - 1;
}
}
void Update()
{
theText.text = textlines[currentLine];
}
}
In this script the points where I have written '.text' do not work. I have done some investigation into this as I have used this script before on another project, but on the other project VS does not call 'UnityEngine.UI' unnecessary. Also: Dragging and dropping the UI Text I am trying to use into 'theText' (Which is what I have named the Text) does not work and I am not sure why as this object does have the Text component attached.
I have no idea whatsoever to why it may not be working and some help would be ever so much appreciated, Thanks.
Answer by Hellium · Oct 02, 2017 at 07:07 AM
I guess you have in your project, one of your classes named Text
. Use the full name of the Unity's Text
class as follow :
public UnityEngine.UI.Text theText;
// ...
theText = GetComponent<UnityEngine.UI.Text>();
I love you so much right now. I thank you a million times. <3
Haha! Glad I helped ;) In Unity, there is a rule of thumb advising against na$$anonymous$$g your custom class the same as Unity's classes.