Change material emission for a second then go back to default

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


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

• On Start() it will only run it 1 time... I need a flicker effect – Warface Feb 12 '16 at 19:40
• SO you want to executes like on and off? 1 second on and 1 second off? or anything else? – Hamza Hasan Feb 12 '16 at 19:43
• Yes and repeats – Warface Feb 12 '16 at 19:45
• Check the edits – Hamza Hasan Feb 12 '16 at 19:48
• 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. – Draco18s Feb 12 '16 at 20:45