Generating Random Numbers on a Game Object

Specifically, I want to make a script that prints a random number (1-10) onto a cube game object. Afterwards, these numbered cubes would be instantiated; in other words, I want to make a script that generates numbered cubes, and the numbers on the cubes are randomized and can be accessed through code (Javascript).

My thought process is to make a 3D text game object and then have the cube be the parent of that game object. However, how do you access and edit the text of the 3D text object in code?


public GameObject textGameObject;

string text = (int)(Random.Value * 10).ToString();

taxtGameobject.GetComponent< TextMesh >().text = text

this should work if im not mistaken but i havn’t tested it