Search Unity

How can I assign texture to gameobjects created in a script?

Discussion in 'Scripting' started by FiloTG, Mar 1, 2015.

  1. FiloTG

    FiloTG

    Joined:
    Mar 1, 2015
    Posts:
    2
    First of all, I've poor skills in English, so I'm sorry if my grammar is awful.

    I'm messing around with an idea which implies procedural world generation. My first baby steps in the project were something like this little script:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class TerrainGenerator : MonoBehaviour {
    5.     // Use this for initialization
    6.     void Start () {
    7.  
    8.         TerrainData tData = new TerrainData();
    9.         tData.size = new Vector3(512,512,512);
    10.         var x = tData.heightmapWidth;
    11.         var z = tData.heightmapHeight;
    12.         var hmap = tData.GetHeights (0, 0, x, z);
    13.         for (int i = 0; i < x; i++) {
    14.             for (int j = 0; j < z; j++) {
    15.                 hmap[i,j] = (float)(0.01*(1+(i%2)))+((float)((j%2)*0.01));
    16.             }
    17.         }
    18.         tData.SetHeights (0, 0, hmap);
    19.         GameObject terrain = Terrain.CreateTerrainGameObject(tData);
    20.  
    21.     }
    22.  
    23. }
    24.  
    This generates a plain terrain, and then edit some heights in order to get a regular pattern.

    My next goal is to apply some type of texture or colour. Navigating I found that what I need is to create a material. But I need a renderer for it. So my approach is something like this:

    Code (CSharp):
    1.     void Start () {
    2.  
    3.         Texture2D tex = Resources.Load("mytextimage.jpg") as Texture2D;
    4.         TerrainData tData = new TerrainData();
    5.         tData.size = new Vector3(512,512,512);
    6.         var x = tData.heightmapWidth;
    7.         var z = tData.heightmapHeight;
    8.         var hmap = tData.GetHeights (0, 0, x, z);
    9.         for (int i = 0; i < x; i++) {
    10.             for (int j = 0; j < z; j++) {
    11.                 hmap[i,j] = (float)(0.01*(1+(i%2)))+((float)((j%2)*0.01));
    12.             }
    13.         }
    14.         tData.SetHeights (0, 0, hmap);
    15.         GameObject terrain = Terrain.CreateTerrainGameObject(tData);
    16.         terrain.AddComponent ("MeshRenderer");
    17.         terrain.renderer.material.SetTexture ("_MainTex", tex);
    18.  
    19.     }
    20.  
    When I run the game, nothing changes. I tried other things, but I think the real problem is a missconception of what should I generate.

    To sum up, my question is: What should I change/add to my code in order to apply my texture to the GameObject "terrain"? And why?

    Thank you for reading so far, and thanks again for your answers

    P.S: I tried to choose the most appropriated subforum to my doubt, but I'm not sure if this thread should be in Graphics.
     
  2. Juice-Tin

    Juice-Tin

    Joined:
    Jul 22, 2012
    Posts:
    244
    Code (CSharp):
    1. terrain.renderer.material.mainTexture = tex;
    This is what you want to use.

    Many materials can have multiple textures in them. They can sometimes give them specific names. SetTexture is to change a texture with a specific name, in your case '_MainTex', which your material might not even have. mainTexture however will always target the first texture.

    Edit: You may want to also try using Texture instead of Texture2D, if the above doesn't help.
     
  3. FiloTG

    FiloTG

    Joined:
    Mar 1, 2015
    Posts:
    2
    First of all, thank you for answering. I tried what you said, and nothing changed. But when I debugged I found that the "tex" object is null. I tried to use tex as a public attribute and then placing my image using the inspector view, but it didn't work.

    Any ideas? I will expand my post if more information is needed

    Edited: I found the problem I had loading (Resources folder ^^U), but I still got a white terrain, and I have no idea why...
     
    Last edited: Mar 1, 2015