Question about random text in TextMesh

hello,
i am a total beginner with unity and have very little knowledge in programming.

for a game scene i would need to pull random texts to be displayed in a textmesh canvas.

could somebody help me with a beginner friendly how-to this?

i saw this posting:

Here it is recommended to use these codes:

 var snippets = ["hello", "good day", "what's up"];

//random number between 0 and length-1
var index = Random.Range(0, snippets.Length);


3)

```csharp
 GetComponent(TextMesh).text = snippets[index];
 function Start() {
     var snippets = ["hello", "good day", "what's up"];
     var index = Random.Range(0, snippets.Length);
     GetComponent(TextMesh).text = snippets[index];
}

Sorry for asking such basics, but how can i implement this in detail?

I tried to create a new script and called in RandomText.cs

    using System.Collections.Generic;
using UnityEngine;

[System.Serializable]
public class RandomText
{



var snippets = ["hello", "good day", "what's up"];


     //random number between 0 and length-1
var index = Random.Range(0, snippets.Length);


     GetComponent(TextMesh).text = snippets[index];



         function Start() {
     var snippets = ["hello", "good day", "what's up"];
     var index = Random.Range(0, snippets.Length);
     GetComponent(TextMesh).text = snippets[index];
}


}

But this is giving errors that var and GetComponent are not allowed to used.

Also after creating the script, is it just to add the script to the TextMesh object in the canvas?

Hope you can help me and sorry for asking such stupid/basic things!

All the best,
Mark

I recommend you do not use the 3D Text which is the component as this is the old legacy text component which is no longer on active development. Instead use the TextMeshPro component as seen below which is a more feature rich and powerful text component that is still very much on active development.

6859862--799829--upload_2021-2-20_14-49-16.png

After you install the TMP Essential Resources, you should also import the TMP Examples & Extras which includes script examples that will show you how to add and access the text component via scripting.

thx for the info! appreciated

i was able to solve this with the help of somebody from stackoverflow.

if somebody has a similar issue, here is the code used:

using UnityEngine;
using TMPro;


public class RandomText1 : MonoBehaviour
{
    public string[] words = { "XA", "XB", "XC" };
    public TMP_Text text;

    private void Start()
    {
        // log whatever comes out of the RandomWord string.
        string wordToDisplay = RandomWord();

        text.text = wordToDisplay;
    }

    // when you see a string function,
    // it will return a string that
    // you can use anywhere!
    private string RandomWord()
    {
        // grab a random string from the words array
        string randomWord = words[Random.Range(0, words.Length)];

        // return it (this will be the string that the script will use)
        return randomWord;
    }

}