0
\$\begingroup\$

I'm looking for a way to change the emission of a material for a second then get it back to where is was by default

Here's what I have for now

float emission_lvl = 5.0F;
float emission_lvl_start = 0.5F;

public Update(){
    mat.SetFloat("_Emission",emission_lvl);
    Invoke ("SetEmissionBack", 1.0f);
}

public void SetEmissionBack(){
    mat.SetFloat("_Emission",emission_lvl_start);
}
\$\endgroup\$
4
\$\begingroup\$

Well, your placement is wrong. Execute same code of Update in Start or whenever you want to execute it and remove it from Update. Because Update is a loop that executes 30 - 60 (depends) per second and mat.SetFloat("_Emission",emission_lvl); executing per frame, that is preventing to change material's emission property.

And now I came to know that you want flicker effect. So you can use Coroutine for that.

void Start()
{
    StartCoroutine("StartEffect");
}

IEnumerator StartEffect()
{
    while(true) // Or any other condition, this will repeat until this condition is true
    {
        mat.SetFloat("_Emission",emission_lvl);
        yield return new WaitForSeconds(1.0f);
        mat.SetFloat("_Emission",emission_lvl_start);
        yield return new WaitForSeconds(1.0f);
    }
}
\$\endgroup\$
  • \$\begingroup\$ On Start() it will only run it 1 time... I need a flicker effect \$\endgroup\$ – Warface Feb 12 '16 at 19:40
  • \$\begingroup\$ SO you want to executes like on and off? 1 second on and 1 second off? or anything else? \$\endgroup\$ – Hamza Hasan Feb 12 '16 at 19:43
  • \$\begingroup\$ Yes and repeats \$\endgroup\$ – Warface Feb 12 '16 at 19:45
  • \$\begingroup\$ Check the edits \$\endgroup\$ – Hamza Hasan Feb 12 '16 at 19:48
  • 1
    \$\begingroup\$ You may want to use MaterialPropertyBlock for this. As-written this will create a new material instance once every second, requiring the old (now unused) copies to get garbage collected. \$\endgroup\$ – Draco18s Feb 12 '16 at 20:45

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.