- Home /
Problems exposing 4.6 Text to the inspector as public Text
Hi, I am using 4.6 and trying to expose a text component to the inspector. The thing is, my script works fine on one of my other projects, but refuses to work in this one, despite being able to expose a UI image just fine.
Basically, If I use this code:
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public Class UIControl : Monobehaviour (
public Image healthImage;
public Text ammoCount;
public int ammoRemaining;
void Start()
{
healthImage.fillAmount = 1.0f;
ammoCount.text = "ammo" +ammoRemaining;
}
The weird thing is that the health image is exposed to the inspector, but the Text isn't.
So, I thought I would change the code to use GetComponent to reference the text:
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public Class UIControl : Monobehaviour (
public Image healthImage;
public GameObject textHolder;
Text ammoCount;
public int ammoRemaining;
void Start()
{
ammoCount = textHolder.GetComponent<Text>();
healthImage.fillAmount = 1.0f;
ammoCount.text = "ammo" +ammoRemaining;
}
If I try that, I get the following error:
The type 'Text' cannot be used as a type parameter 'T' in the generic type or method 'UnityEngine.GameObject.GetComponent()'. There is no implicit reference conversion from 'Text' to 'UnityEngine.Component'.
Does anyone have any ideas? I cant really understand why the Image works and the text doesn't?
Answer by Kiwasi · Nov 28, 2014 at 07:26 PM
Sounds like you have another class in your assembly named Text. To find it right click on Text and choose 'go to declaration'
You can either rename the other class, or use the fully qualified name.
public UnityEngine.UI.Text myText;
Aha! It was an asset on the store mucking things up for me! thanks for the help :)