- Home /
How to know when field value was edited? (Can't use property!)
I want to create SpriteGroup script that will update every SpriteRenderer in child objects. Let's say I have a parent with SpriteGroup script holding 3 sprites.
Then I want to have Color field in SpriteGroup that will update Color field in all SpriteRenderer components in children under this group.
How do I achieve it? It should also work with Animation, I want to animate whole object going transparent by editing this single Color field in SpriteGroup and that would edit also all Color fields in SpriteRenderer's
I've written something like this but I don't know if its right direction
public class SpriteGroup : MonoBehaviour
{
SpriteRenderer[] spriteRenderers;
public Color color;
void Start()
{
spriteRenderers = GetComponentsInChildren<SpriteRenderer>();
}
public void ColorChange() //Call this method then 'color' is edited'
{
foreach (var renderer in spriteRenderers)
{
renderer.color = color;
}
}
}
Answer by myzzie · Nov 23, 2020 at 05:47 PM
OnValidate() does exactly that.
private Color color;
public void ChangeColor(Color color)
{
this.color = color;
ColorChange();
}
Like that?
Your answer
Follow this Question
Related Questions
Serialize [,] 1 Answer
2d Spaceship editor? 0 Answers
Assigning a Sprite to a Script and creating a Gameobject out of that sprite 1 Answer
Equivalent of OpenFilePanel without using UnityEditor? 5 Answers
Unity 2019.3 overheating 0 Answers