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?


private void SetGlow()
    line.materials.Length = 2;
    line.materials[1] = "/new/material/directory/material.mat";
  • \$\begingroup\$ 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. \$\endgroup\$
    – 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;
        line.material = mat1;

Refer to Line Renderer for more info.

  • \$\begingroup\$ In this case I would want either 1 material, or both materials to be active. I currently have both active via the inspector, though cannot seem to figure out how to toggle just one of them. Though thank you for your post, that info will be handy for another object I have. \$\endgroup\$ Feb 9 '15 at 4:13
  • \$\begingroup\$ @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" ? \$\endgroup\$
    – Varaquilex
    Feb 9 '15 at 4:30
  • \$\begingroup\$ Odd, no I'm literally using 2 materials at the same time, and it works. Both materials are successfully rendered on the same line. imgur.com/a/Oipdg \$\endgroup\$ Feb 9 '15 at 6:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.