# Adding materials to a Line Renderer through code?

I have a material that I am using to add a glowing effect to my line renderer. I can add this as a 2nd material to the line to give it a glowing effect.

I want to be able to add/remove this material through code, so I can essentially enable/disable the effect. I have no clue how to access the current materials of the line renderer in a way that allows me to add/remove elements from the materials array. How can I do that?

Pseudocode:

private void SetGlow()
{
line.materials.Length = 2;
line.materials[1] = "/new/material/directory/material.mat";
}

• have you tried Material[] mats = {material1, material2}; line.materials = mats; ? I think because of the way Unity's getters/setters work, you can only assign to the whole array, rather than trying to modify a single index at a time. – DMGregory Jun 9 '15 at 13:35

You can have 2 public materials, assign the materials to them from the inspector and alternate between them.

LineRenderer line;
public Material mat1, mat2;

// Use this for initialization
void Start()
{
line = GetComponent<LineRenderer>();
}

private void ToggleGlow()
{
if (line.material == mat1)
line.material = mat2;
else
line.material = mat1;
}


• @douglasg14b Both materials cannot be active at the same time according to the manual: Materials: The first material from this list is used to render the lines. If you have two elements in your line renderer's materials array, only the first one will be active at all times. And what do you mean by "toggling only just one of them" ? – Varaquilex Feb 9 '15 at 4:30