How to render an arbitrary string during runtime

My problem is essentially that I want to take a completely arbitrary string from outside my game assets (notionally an XML file which I parse with a custom parser - this part shouldn’t be an issue, I’m just mentioning it for context) and then plug this string into some manner of game object that displays text.

I know this is a fairly simple thing to do, but for the life of me I cannot figure out how to do it.

As far as I can tell, it’s something like:

Get string
Create game object
Use string as parameter in game object

Or alternatively:

Get string
Create prefab
Use gamecomponent to change prefab string value

But I couldn’t figure out how to actually implement either of these, and it appears to be something so simple that nobody else has asked the question.

For some additional context, I’m trying to implement something I can use to print large chunks of textual narrative (similar to CRPGs like Expeditions: Conquistador or Pillars of Eternity, or Visual Novels) which don’t require editing of the scripts to take place in Unity itself, so I can blackbox the technical parts and not confuse less technically-minded team-members while still allowing them to rapidly change and test iterations of the script.

Thus, XML script loaded by the game and parsed with a custom parser and then rendered.

In any case, thank you for taking the time to read this, and I hope you can help me!

If you want to display text on screen (like visual novels, dialogue systems, etc do), I suggest using one of Unity’s UI systems.

If you want to use the legacy UI system, add an OnGUI method to a MonoBehaviour of your choice:

// On GUI callback. Called by Unity
void OnGUI()
{
    // Screen space position (from top left)
    Rect displayArea = new Rect(5, 5, 400, 300);
    // Show text (not changeable)
    GUI.Label(displayArea, textYouWantToDisplay);
}

textYouWantToDisplay corresponds to one of the strings you got from parsing the xml.

Second possibility is to use Unity’s new UI. You create a new Canvas in the scene (find it in Create>UI), add a new Text (Create>UI). Then add a script that updates the Text (e.g. TextDisplay) and pass a reference of said Text to an instance of TextDisplay (in the inspector). The script would roughly look like this:

using UnityEngine;
using UnityEngine.UI; // Text is in this namespace
using System.Collections;

public class TextDisplay : MonoBehaviour
{
    public Text display;


    public void ShowText(string textYouWantToDisplay)
    {
        if(display)
        {
            display.text = textYouWantToDisplay;
        }
    }
}

Of course, you have to call ShowText from your parser and use the strings you loaded from xml as parameters.

If you want text that is placed in 3D like a GameObject, add a 3D Text to
your Scene (Create>3D Object>3D Text). In the script above, change the type of display to TextMesh and pass a reference to the 3D Text to an instance of this script somewhere in the scene.