[Serializefield] Text vs String

I’m still confused about the use of the variable types Text and String.

In the previous lesson, we wrote something like this:

public class GoneMissing : MonoBehaviour
{

    [SerializeField] Text textComp;

    void Start()
    {
        textComp.text = ("Story");
    }


    void Update()
    {
        
    }
}

But in the scriptable Object script we have something like this:

[CreateAssetMenu(menuName = "State")]
public class State : ScriptableObject
{
   [TextArea(14,13)]  [SerializeField] string storyText;
}

What’s the precise difference between Text and String?

Hi,

string is a base type from C# library.
Text is a type from the Unity Engine library.

Text would be used when you are trying to access a UI Text GameObject within a scene for example.

It has a property of the same name, text, this holds the actual text that you want to display, this is the string data.

In your scriptable objects you have no reference to the UI Text GameObject(s) in the scene, these are being used to store data. The data type for your story text is string.

Hope this helps :slight_smile:


See also;

1 Like

Thanks Rob! Crystal clear.

1 Like

You’re very welcome, added a couple of links for you too :slight_smile:

1 Like

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms