0

I am trying to change the texture of my object with this code:

Texture2D baileyburlwood = Instantiate(Resources.Load("bailey burlwood") as Texture2D);
    myObject.GetComponent<Renderer>().material.mainTexture = baileyburlwood;

It is working perfectly fine in the editor, the texture changes but when I tried to run it in my android device, my object just goes black. There is also no error or any warning. Pls help! Thanks!

I am using Unity 5.5.1f btw

I added a screenshot of where the file is located

Janella
  • 41
  • 1
  • 1
  • 5

1 Answers1

0

From the screenshot in your updated question, the image you want to load is called bailey burlwood.jpg which is already in the Resources folder..

Herein lies the problem:

Instantiate(Resources.Load("bailey burlwood") as Texture2D);

You instantiate prefabs, scripts and components not normal classes like Texture2D.

Your code would have worked if bailey burlwood.jpg is bailey burlwood.prefab and you load it with GameObject prefab = Resources.Load("shipPrefab", typeof(GameObject)) as GameObject; but that's not the case here.

Since the "bailey burlwood" file is a JPG file, you should load like this:

Texture2D baileyburlwood = Resources.Load("bailey burlwood") as Texture2D;
myObject.GetComponent<Renderer>().material.mainTexture = baileyburlwood;

Note that there is no Instantiate function involved. See this post for how to load other image files with different image settings when using the Resources folder.

Community
  • 1
  • 1
Programmer
  • 121,791
  • 22
  • 236
  • 328