loading images

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:

  1. UnityEngine.Resources:Load(String) (at C:\BuildAgent\work\842f9557127e852\Runtime\ExportGenerated\Editor\BaseClass.cs:1310)
    Pla:.ctor()

  2. 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 ()

  3. NullReferenceException in line: tex.LoadImage(bindata.bytes);

Anyone knows why or a solution to load images?

Thanks

See the docs for Resources.Load: “The path is relative to any Resources folder inside the Assets folder of your project, extensions must be omitted.” Also, all code must be inside a function.