Is there any way to generically use Text or GUIText?

Here’s what i’m trying to do:

GUIText text0;
Text text1;


Object Component()
{
	if (mode == 0) return text0;
	return text1;
}

Component().text = somestring;

Basically i want to create a script that will work whether the attached textual component is a UI.Text or a GUIText. I’d like it to be blind, and not care. Both of those types share a “text” property anyway, and it works identically. so i just want to set it and not care which type is underlying.

No, it’s not possible the way you want to do it. Those are two seperate classes and just because their property is named the same doesn’t mean it can be accessed the same way. If both classes would implement a common interface, like this one:

public interface IHasTextProperty
{
   string text {get; set;}
}

it would be possible but since they don’t, there’s no way to get a single reference to “something” and directly access those two different text properties.

The closest you can come to is:

GUIText text0;
Text text1;

public void SetText(string aText)
{
    if (mode == 0)
        text0.text = aText;
    else
        text1.text = aText;
}

public string GetText()
{
    if (mode == 0)
        return text0.text;
    else
        return text1.text;
}

or as property:

GUIText text0;
Text text1;

public string text
{
    get
    {
        if (mode == 0)
            return text0.text;
        else
            return text1.text;
    }
    set
    {
        if (mode == 0)
            text0.text = value;
        else
            text1.text = value;
    }
}