- Home /
Object turns pink when changing material at real time:
Im using quads with "Sprite/diffuse" shading on the materials for a 2D game.
im trying to load from the materials folder using C#:
Megaderp= Resources.Load("Megaderp", typeof(Material)) as Material;
Left =Resources.Load("Player",typeof(Material)) as Material;
Right =Resources.Load("Left", typeof(Material)) as Material;
in a different attempt, I tried this to specify where the folder the materials are located:
Megaderp= Resources.Load("Materials/Megaderp", typeof(Material)) as Material;
Left =Resources.Load("Materials/Player",typeof(Material)) as Material;
Right =Resources.Load("Materials/Right", typeof(Material)) as Material;
when trying to render the new material im doing:
if (velocity.x < 0) {
GetComponent<Renderer>().materials[0] = Left;
}
I have also tried different methods of rendering it but the result is usually the same with each attempt, apperantly pink usually means that a file is missing, so my best guess is that im either loading the Resources wrong or im just rendering it incorrectly. (or both!)
Thanks!
Answer by Tasarran · Apr 07, 2017 at 07:39 PM
Good news, should be a simple fix!
https://docs.unity3d.com/ScriptReference/Resources.Load.html
The path you are loading from has to either be a folder named 'Resources', or underneath a folder named 'Resources'.
Didnt realize anyone had replied to my question! I eventually realized this after reading through the API some more but thanks a bunch, this was exactly the fix that I needed all that time ago.