Sign up ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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.

Pseudocode:

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

I'm open to suggestions.

Thanks!

share|improve this question
    
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 at 13:35

1 Answer 1

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;
}

Refer to Line Renderer for more info.

share|improve this answer
    
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. – douglasg14b Feb 9 at 4:13
    
@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 at 4:30
    
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 – douglasg14b Feb 9 at 6:33

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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