1
\$\begingroup\$

I'm working on a unity game where I want to change the color of a material from within a script. The material applies to a line renderer.

I currently have the following script:

using UnityEngine;
using System.Collections;

public class LaserProperties : MonoBehaviour {
    public Vector3 RGBStrengths;
    public LineRenderer lineRenderer;

    /*
     * returns the color in RGB.
     */
    Color getColor(){
        Color c = new Color (0, 0, 0, 1);
        float highest = Mathf.Max (RGBStrengths.x, RGBStrengths.y, RGBStrengths.z);
        c.r = RGBStrengths.x / highest;
        c.g = RGBStrengths.y / highest;
        c.b = RGBStrengths.z / highest;
        return c;
    }
    /*
     * returns the total amount of energy in the beam 
     */
    float getStrength(){
        return RGBStrengths.magnitude;
    }
    void Update(){
        updateBeam ();
        Debug.Log (getColor());
    }
    /*
     * set color of the beam to the color of the laser 
     */
    public void updateBeam(){
        lineRenderer.SetWidth (getStrength (), getStrength());
        lineRenderer.material.color = getColor ();
        lineRenderer.material.SetColor ("_Albedo", getColor ());
        lineRenderer.material.SetColor ("_Emission", getColor ());

    }
}

And here is the material a laser material.

However changing the properties does not result in any chance to the material in question.

Any idea what I'm doing wrong?

\$\endgroup\$
  • \$\begingroup\$ You haven't told us what the symptom you're trying to resolve is, or what intended behaviour you want to create instead. \$\endgroup\$ – DMGregory Jun 11 '15 at 2:17
1
\$\begingroup\$

I tested your script and was able to dynamically adjust the colors of the line renderer. Perhaps you didn't pass your linerenderer component into your script in the editor on the attached object? Alternatively, you could add to your script:

void Start() {
    lineRenderer = gameObject.GetComponent<LineRenderer>();
}

Aside from that your code seems to work fine.

\$\endgroup\$
0
\$\begingroup\$

When using Unity 5 and the Standard shader you need to use "_EmissionColor" rather than "_Emission" to set the Emission value.

\$\endgroup\$

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.