- Home /
Question by
a2amazing4me · Jun 14, 2020 at 12:52 PM ·
c#color change
material.color doesn't change black
I want to make the enemy and player flash red when hit, however where there is black in the character that part remains black and does not change to red.
IEnumerator damageFlash()
{
Color32 c = this.GetComponent<Renderer>().material.color;
this.GetComponent<Renderer>().material.color = Color.red;
yield return new WaitForSeconds(0.1f);
this.GetComponent<Renderer>().material.color = c;
}
this is the code i used for flashing color
Comment
Answer by rohandoesunity · Jun 18, 2020 at 06:52 PM
Did you call the method using StartCoroutine()
?
this code seemed to work for me.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class FlashRed : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
StartCoroutine(damageFlash()); // start the coroutine
}
IEnumerator damageFlash () {
Renderer renderer = GetComponent<Renderer>(); // get renderer
Color c = renderer.material.color; // get the previous color
renderer.material.color = Color.red; // set color to red
yield return new WaitForSeconds(0.1f); // wait for 0.1 seconds
renderer.material.color = c; // set color back to previous color
}
}
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
How can I change a tile color in Unity by using C#., 2 Answers
How can I loop through all colours in code? 1 Answer
[HELP]Changing Color fill of slider on button clicked 0 Answers