How to change a child's shader to another shader?
Hello,
So I've got some objects I've created on the fly representing my player's units. I'd like to change the unit's color based on who the unit belongs to. I've created a dark purple shader and tried applying it to the child object of my script like such:
this.gameObject.transform.GetChild (0).GetComponent<Renderer> ().materials [0].shader = Shader.Find ("PlayerTwo");
Unfortunately the code above changes the cube to a bright pink color. I've tried other colors (red, orange, etc...) to make sure it isn't a lighting problem... but to no avail.
Example cube: http://nerd.guru/paste/Unity_2016-03-19_21-17-50.png
What am I doing wrong?
Answer by vapi · Mar 20, 2016 at 01:11 PM
Hi,
Bright pink means shader error. Look in your console. Most probably it cannot find the shader by the name you specified.
However, instead you should pre-make all kinds of materials you are going to use in the scene, set up all the colors and variables, and in runtime you call this on the child object:
.GetComponent().sharedMaterial = thatPremadeMaterialYouMade;
Your answer
Follow this Question
Related Questions
Problem with fresnel materials 0 Answers
Standard Shader Emissive Warning? 2 Answers
Outline Shader working in editor but not in-game 0 Answers
Similar looking base and globe model 0 Answers
Changing color on specific tile in texture at runtime 0 Answers