OK so I can do this to load a texture from my resources folder and apply it to a model…
Texture thetexture = Resources.Load(“IMG_0317”) as Texture;
myobject.GetComponent().material.mainTexture = thetexture;
but “thetexture” is loaded from Resources. Let’s say I don’t need to load it and just set mainTexture and all I have is the name of the texture I’m loading (IMG_0317). I can’t figure out how to get that texture into a gameobject variable so I can assign it. I can do it with a model like so:
myobject = GameObject.Find(“Cube”);
I need to do something like that with the name of a texture. All the C# code i’ve found online seems to be outdated.
In such a case you must store the texture somewhere after you’ve loaded it from Resources, something like this should work:
public Renderer targetRenderer;
public List< Texture2D > myTextures;
public void Awake() {
myTextures = new List< Texture2D >();
Texture2D t = (Texture2D)Resources.Load( "blablabla/blabla/bla.png" );
myTextures.Add( t );
}
public void SetTextureByName( string filename ) {
for( int i = 0; i < myTextures.Count; i++ ){
if( myTextures[ i ].name == filename ) {
targetRenderer.material.mainTexture = myTextures[ i ];
break;
}
}
}
In this example you would load all textures you wish to be available in Awake, and add them to the texture list, the SetTextureByName member can then be used to loop through all indices of the list to see if there is a texture of the specified name