I wanted to set the colors in runtime in Unity. When I set, it returns a purple color instead. Following is the code in my start method;
Material testMaterial= Resources.Load("Assets/Colors/front/LA.mat", typeof(Material)) as Material;
objectRenderer.GetComponent<Renderer>().material = testMaterial;
I develop my game in Unity URP and material apply to both side of the game object. I can do it manually by drag and drop but I wanted to automate it. When I do it with C# script, I get this purple color. Thanks for any hint.
CodePudding user response:
You should add a new folder called "Resources". Unity understands your path like "Resources/Assets/Colors/front/LA.mat", but there's no Resources folder in your project. Just create Resources folder and move all assets you need to load at runtime to it. But be careful and not move all the assets to this folder because these resources will be included in build even if you are not using them, so this may cause build size grow.
CodePudding user response:
Did you add the material to the Resources folder?
https://docs.unity3d.com/ScriptReference/Resources.Load.html