- Home /
Adding a texture to terrain through code c#
I've researched this issue through all the other examples and pulled the working code correctly, yet I continue to get the same error. Here is my current code:
public class RandomTerrainAddTexture : MonoBehaviour {
private TerrainData tData;
void Start () {
tData = Terrain.activeTerrain.terrainData;
SplatPrototype[] terrainTexture = new SplatPrototype[1];
terrainTexture[0] = new SplatPrototype();
terrainTexture[0].texture = (Texture2D)Resources.Load("Assets/MyTextures/PossibleRealGrass1");
tData.splatPrototypes = terrainTexture;
}
}
And here is the error that I absolutely cannot seem to get rid of:
Terrain splat 0 is null.
UnityEngine.TerrainData:set_splatPrototypes(SplatPrototype[])
RandomTerrainAddTexture:Start() (at Assets/MyScripts/RandomTerrainAddTexture.cs:30)
The path is correct and I've tried altering it a ton of different ways and still the same error. Any ideas would be greatly appreciated. Thank you!!
So this is really about how to find an existing texture? Seems like Terrain isn't really the issue.
You could also add public Texture2D someGrass;
, drag grass.jpg into the inspector, and use tt[0] = someGrass;
. That can make it easier, esp. for non-coders, to Drag around different textures.
Answer by _-_ · May 25, 2013 at 10:48 AM
I've struggled for several hours with the exact same problem yesterday. And......... I've found a solution!
Right now, your texture is in this path ("Assets/MyTextures/PossibleRealGrass1"), right? You need to create a map (EDIT: folder) called resources, and put your texture in there. For example, if you would put your texture in "resources/MyTextures/PossibleRealGrass1", you need to call it as follows:
terrainTexture[0].texture = (Texture2D)Resources.Load("MyTextures/PossibleRealGrass1");
Because, apparently, Resources/Load will load from the map (EDIT: folder) "resources".
It is a real shame that trying to create a terrain from code is so poorly documented. It's guaranteed to give you some headaches, as I've experienced. I think I've sorted out most of it, so let me know if you have any other questions or need more info on this one (I'm not sure if I've explained it well enough).
EDIT: In my language a folder is called a "map". Probably the fact that terrains are so closely related to maps ( heightmaps, alphamaps, splatmaps and so forth) caused me to make this confusing mistake. So I meant create a FOLDER called resources. My bad, sorry.
Really really appreciate the response this has had me stuck for a while so I've just been working around the currently randomly generated terrain remaining untextured.
Anyhow, not 100% on what you mean create a "map" called resources? I know that when you build your program it generates the folder Resources in the reference folder used to run the EXE, but I'm not quite sure as to how to create a map in that sense I guess. Thanks so much for helping me out!
Your answer
Follow this Question
Related Questions
Distribute terrain in zones 3 Answers
Brushes like in unity built-in terrain. Mouse projection on mesh in edit mode 1 Answer
Multiple Cars not working 1 Answer
Change terrain texture colour 1 Answer
rotate normal map 1 Answer