Search Unity

Change Transparent Material Color

Discussion in 'Scripting' started by MrMetwurst2, Oct 27, 2016.

  1. MrMetwurst2

    MrMetwurst2

    Joined:
    Jul 16, 2009
    Posts:
    253
    G'day guys,

    I've spent 2 days on this and it's doing my head in!
    Using Unity 5.3.6

    I have a particle system where the color of the particle is set by the material in the ParticleSystemRenderer.
    I have a base material that I am copying that has the following properties...

    material.png

    Now I need each object in the games trails to match it's base color so as a test i used the following code...

    Code (CSharp):
    1. public class enemyMaterial : UnityEngine.MonoBehaviour
    2. {
    3.     public UnityEngine.Material material = null;
    4.  
    5.  
    6.     public void Start()
    7.     {
    8.         UnityEngine.Material newMaterial = null;
    9.  
    10.         newMaterial = new UnityEngine.Material(material);
    11.         newMaterial.SetColor("_Color", new UnityEngine.Color(1f, 0f, 0f, 0.04f));
    12.         newMaterial.name = "TestRed";
    13.         GetComponent<UnityEngine.ParticleSystemRenderer>().material = newMaterial;
    14.     }
    15. }
    16.  
    But the color will not change.
    I tried _Color, _TintColor, _Albedo (out of desperation) but all don't work.
    _EmissionColor kind of works but it's only adding a separate color on top of the base one.

    Why won't the color change to Red in that code?

    I've checked the Material property on the Particle Renderer and made sure it has the "TestRed" material that I create in this code.

    Please help me!

    Thanks.
     
    Last edited: Oct 27, 2016
  2. takatok

    takatok

    Joined:
    Aug 18, 2016
    Posts:
    1,496
    I'm fairly certain these lines here:
    Code (CSharp):
    1. UnityEngine.Material newMaterial = null;
    2. newMaterial = new UnityEngine.Material(material);
    Should have Unity spit out a Null Reference Exception

    You could just directly get the material from the Renderer and change its color:
    Code (CSharp):
    1. void Start()
    2.     {
    3.         UnityEngine.Material newMaterial = GetComponent<UnityEngine.ParticleSystemRenderer>().material;
    4.         newMaterial.SetColor("_Color", new UnityEngine.Color(1f, 0f, 0f, 0.04f));
    5.         newMaterial.name = "TestRed";
    6.     }
    also if you just stick:
    using UnityEngine;

    at the top of your script you can leave out all the UnityEngine.Material and just type Material
     
  3. MrMetwurst2

    MrMetwurst2

    Joined:
    Jul 16, 2009
    Posts:
    253
    No it doesn't because the material is being copied from the original material via the editor.
    "material" is a public variable.

    I've tried this and many other combinations to try and make this work. All failing to change the colour.
    Try it yourself and let me know if it works.

    This is just the way Nottorus Visual editor is producing the code. Not a concern at this stage. I'm just trying to get the colours to change! :/

    Thanks for the feedback though. I do appreciate your help.