Profile image

Change Material color in script?

8,728 BigBushy  3.1 years ago

I been working on a mod that just includes more parts like lights, scopes, and cameras, but currently im stuck on how to update the material of the light mesh to the color of the light. I tried loading the material asset with

"Mat = ServiceProvider.Instance.ResourceLoader.LoadAsset<Material>("Assets/Materials/LightMat.mat");"

and then changing the EmissionColor and Color with

Mat.SetColor("Color", rbg);
Mat.SetColor("EmissionColor", rbg);

(the 2 have underscores before Color and EmissionColor)

(rbg is the color of the light saved into a variable)

but for some reason when i load the mod, the color of the material is pink and stays like that no matter if the part is enabled or not. I tried other methods aswell and it's been driving me crazy for the past few days. any idea on how to update materials?

  • Log in to leave a comment
  • Profile image
    8,728 BigBushy

    @Jester do you have a discord? I would love to learn some more modding from you

    3.0 years ago
  • Profile image
    313k Gestour

    Ok, glad to hear you got it working.

    3.1 years ago
  • Profile image
    8,728 BigBushy

    @Gestour Yea i actually know some basics of unity and actually managed to get the other parts set up fairly easily. I Recently found the solution, and boy was i fuming when i found out that all i needed to do was add Mat.EnableKeyword("_EmissionColor");
    there were no videos on how to change the emission color and only managed to find the solution on Unity Answers

    +1 3.1 years ago
  • Profile image
    313k Gestour

    It's too hard to explain here. You need a tutorial.
    You should read the Unity3d Manual, and do more than a couple tutorials probably before you try to make mod parts.
    Changing a material is as basic as it gets in unity.
    Learn the basics first, then I will gladly help.
    Unity Learn has lots of good free tutorials, so does youtube.

    3.1 years ago