- Home /
Having trouble resetting the color of an object after the trigger leaves
I have a function written to cause my gameobjects to pulse in color when the enter certain triggers. However, I can't get them to return to the original color after they leave the trigger.
private void OnTriggerExit(Collider col)
{
if (isServer)
{
switch (col.name)
{
case "pulseTrigger(Clone)":
returnColor(col);
break;
}
}
}
public void returnColor(GameObject c)
{
if (pulseDict[c.gameObject] == true)
{
float r = 0f;
float g = 0f;
float b = 0f;
foreach (KeyValuePair<GameObject, bool> x in pulseDict)
{
if (x.Value == false)
{
r = x.Key.GetComponent<SpriteRenderer>().color.r;
g = x.Key.GetComponent<SpriteRenderer>().color.g;
b = x.Key.GetComponent<SpriteRenderer>().color.b;
break;
}
}
c.gameObject.GetComponent<SpriteRenderer>().color = new Color(r, g, b, .75f);
pulseDict[c.gameObject] = false;
}
}
I use these function along with a dictionary that keeps track of which objects have already changed color. I know that the rgb values are correct and the dictionary is updating correctly so I cannot figure out why the color will not update. If anyone could help it would be greatly appreciated.
First step: make sure your code is being called. After if (pulseDict[c.gameObject] == true)
add a line Debug.Log("returning color of object " + c.name);
Confirm you see this debug line displayed in the output window when you run the project. If you don't, add some more debug's to see why.. eg.. Debug.Log("object " + c.name +" does not exist in the dictionary");
or Debug.Log("object is not named pulseTrigger(Clone), its name is actually: " + c.name);
Your answer
Follow this Question
Related Questions
Unity 2020.3 URP shader color won't change properly 0 Answers
How to make a sprite flash different colors? 1 Answer
SpriteRender.color not changing the color 1 Answer
Change material color for one object in Projector 0 Answers
unity2d camera switch color palette with radical transition effect 0 Answers