- Home /
Can't add texture to procedurally generated plane
I'm quite new to Unity and currently have the issue that I can't add a texture to my procedurally generated plane:
function Start () {
//Create landscape
var plane : GameObject = GameObject.CreatePrimitive(PrimitiveType.Plane);
plane.transform.position = Vector3(0.0, 0.0, 0.0);
plane.renderer.material.mainTexture = Resources.Load("planeTex");
}
I created a graphic that is 1024x1024 and saved it as .jpg and than dragged this image into my "assets -> materials" folder in unity. At least this is what I gathered in other articles.
Considering the articles this should be a simple task but somehow the texture is not visible (the plane is white). Any help is appreciated (maybe I'm missing a basic information about textures or something like this).
It would be much better if you had the texture as a variable on your script. This would allow you to assign it in the inspector and ensure that it also gets assigned to your material. Resources.Load
may return null
if the asset is not found.
I tried that as well, but the texture was still not visible. Do you maybe have a link to an example?
You mean like this?
var plane : GameObject = GameObject.CreatePrimitive(PrimitiveType.Plane);
plane.transform.position = Vector3(0.0, 0.0, 0.0);
var mainTex : Texture = Resources.Load("planeTex");
plane.renderer.material.mainTexture = mainTex;
no, more like this:
var texture:Texture2D;
function Start() {
//plane code
plane.renderer.material.mainTexture = texture;
}
This is the standard for inspector assigned variables and is preferred over any dynamic resource loading because of the increased speed it provides.
Answer by fherbst · Sep 05, 2013 at 11:36 AM
As per the documentation for Resources.Load, your texture has to be in a folder called Resources if you want to load it this way. So "Assets > Resources > planeTex" would be a correct path, otherwise it probably won't be found.
Oh boy... I read that before and couldn't find a resource folder, didn't think to add one myself :/ Thank you for the help, this fixed my issue!