I have a primitive Cube and I have to load all images of a folder and put a random texture to the cube.
Code:
TextAsset bindata= Resources.Load(“Assets/Img/1.jpeg”) as TextAsset;
Texture2D tex = new Texture2D(4, 4);
tex.LoadImage(bindata.bytes);
cube.renderer.material.mainTexture = tex;
Console errors:
-
UnityEngine.Resources:Load(String) (at C:\BuildAgent\work\842f9557127e852\Runtime\ExportGenerated\Editor\BaseClass.cs:1310)
Pla:.ctor() -
ArgumentException: Load can only be called from the main thread.
Constructors and field initializers will be executed from the loading thread when loading a scene.
Don’t use this function in the constructor or field initializers, instead move initialization code to the Awake or Start function.
UnityEngine.Resources.Load (System.String path) (at C:/BuildAgent/work/842f9557127e852/Runtime/ExportGenerated/Editor/BaseClass.cs:1310)
Pla…ctor () -
NullReferenceException in line: tex.LoadImage(bindata.bytes);
Anyone knows why or a solution to load images?
Thanks