# Change Shader on This Material Via Code?

How do I change this material to have the Toon/ Lit Outline shader via code at the Start of the scene?

Here is my code that I have as of now, that does not work:

 using UnityEngine;
using System.Collections;

{
public Material hatMaterial;    //This is my character's hat shader - I set this in the Inspector

void Start ()
{

hatMaterial = GetComponent<Material> ();

}
}


GetComponent retrieves Components, shader isn't a component (it doesn't inherit from component) so GetComponent won't work.
It could be due to how you're assigning the hatMaterial - did you drag it from the project view? If so that could be the problem because materials are instanced when the game starts so essentially you'd be modifying another material. If that is the case you'd need to fix it by dragging the specific material being used in the scene OR you could get a instance of a renderer, and use its sharedMaterial property to modify all instances of the material.
• This should work: GetComponent<Renderer>().material.shader = yourshader Oct 27 '16 at 14:54