Need help with creating dynamic textures.

Hello everyone,

I am currently working on a game based on atoms. The game allows you to play around with all kinds of atoms from the Periodic Table. In order to reduce the art assets I want to create textures dynamically and slap it onto the atoms (spheres). I can do this easily in editor using a RenderTexture. However, when I try doing it in script (C#) the text simply doesn't show up in the RenderTexture. Here is my code:

void GenerateTexturesForAtoms()
{
    for (int i = 0; i < atom_list.Count; ++i)
    {
        GameObject cam_text = Instantiate(Resources.Load("CamText")) as GameObject;

        RenderTexture render_texture = new RenderTexture(256, 256, 0);
        render_texture.isPowerOfTwo = true;
        render_texture.Create();

        cam_text.GetComponent<GUIText>().text = "Hydrogen";
        cam_text.GetComponent<Camera>().transform.Rotate(new Vector3(0, 0, 0));
        cam_text.GetComponent<Camera>().transform.position = new Vector3(0, 15, 0);
        cam_text.GetComponent<Camera>().targetTexture = render_texture;

        Material atom_mat = new Material(Shader.Find("Diffuse"));
        atom_mat.SetTexture("_MainTex", render_texture);
        (atom_list *as GameObject).renderer.material = atom_mat;*
 *}*
*}*
*```*
*<p>I just need to figure out how to get the GUIText to render onto my target texture. I think there is something really silly that I am doing. Any help would be great!</p>*
*<p>Thank you very much!</p>*

Okay I think I figured it out, or rather found an alternative.

I used a 3DText (TextMesh) instead. I created a prefab with a Camera, Plane and 3DText. The Plane will hold the material you want to show up on the atom. The 3DText will go on top of this plane. The Camera is then set perpendicular to them and thus captures the 3dText over the plane onto a RenderTexture. At this point I have pretty much what I wanted. I doubt if this is the most efficient way to do it, but works pretty well for my game. Also, make sure you use the 3DText Shader instead of the default one. Its available here. Else your text will appear scrambled when you try to use custom font.

Here is the code I used in case any wants to try it out:

void GenerateTexturesForAtoms()
{
    for (int i = 0; i < atom_list.Count; ++i)
    {
        GameObject cam_text = Instantiate(Resources.Load("CamText")) as GameObject;

        RenderTexture render_texture = new RenderTexture(256, 256, 0);
        render_texture.isPowerOfTwo = true;
        render_texture.Create();

        cam_text.GetComponentInChildren<TextMesh>().text = atom_list*.GetComponent<Atom>().element_name;*
 *cam_text.GetComponent<Camera>().transform.Rotate(new Vector3(-90, 0, 0));*
 <em>cam_text.GetComponent<Camera>().transform.position = new Vector3(90, 10 * i, 0);</em>
 *cam_text.GetComponent<Camera>().targetTexture = render_texture;*
 *Material atom_mat = new Material(Shader.Find("Diffuse"));*
 *atom_mat.SetTexture("_MainTex", render_texture);*
 *atom_mat.SetTextureOffset("_MainTex", new Vector2(0.2f, -0.025f));*
 <em>(atom_list *as GameObject).renderer.material = atom_mat;*</em>
 _*}*_
_*}*_
_*```*_