- Home /
How to trigger udpate in editor mode only when I modify component parameters
Hello!
I made a script to generate gameobjects with random positions an random tints. I added "[ExecuteInEditMode]" at the beginning of the script. And I clear generated objects before re generated them on every update.
void Update()
{
if (Application.IsPlaying(gameObject))
{
// Play logic
}
else
{
// Editor logic
ClearGrid();
CreateGrid();
}
}
My problem is that the script constantly update and I just want it update only when I modify my component parameters ( in inspector windows ). Is it clear ans is there a way to do that?
Thanks!
Answer by FlaSh-G · Apr 02, 2020 at 08:41 PM
It's not exactly meant to be used like this, but OnValidate is triggered whenever you change a value in the inspector. The more elegant, although probably also more complex solution would be a custom inspector.
Hey thanks. OnValidate seems to be a solution. And a custom inspector is a way too complex for my level. So, forgive me if my question is stupide but, if I want to update when I modify a parameters, I must use OnValidate on each parameters? I'm not sur how and where to use it.
In fact I'd like my parameters update independently of others parameters.
here are more details of the code:
void Start()
{
CreateGrid();
}
void CreateGrid ()
{
chaosX = (Spacing.x/2.0f)*chaos;
chaosY = (Spacing.y/2.0f)*chaos;
for (int row = 1; row <= Grid.y; row++) {
for (int col = 1; col <= Grid.x; col++) {
// choose a random object in the list
int n = Random.Range (0, ObjectsList.Length);
GameObject randomObject = ObjectsList [n];
// spawns the instance
GameObject instance = Instantiate (randomObject, transform);
instance.name = "Instance_" + col + "_" + row;
instance.transform.localPosition = new Vector2 ((col - 1)* Spacing.x + Random.Range (chaosX,chaosX*-1.0f),(row - 1)* Spacing.y + Random.Range (chaosY,chaosY*-1.0f));
// tint instances
m_SpriteRenderer = instance.GetComponentInChildren<SpriteRenderer>();
m_SpriteRenderer.color = new Color (
(Random.Range (-0.5f, 0.0f) * ColorVariation +1),
(Random.Range (-0.5f, 0.0f) * ColorVariation +1),
(Random.Range (-0.5f, 0.0f) * ColorVariation +1),
1.0f);
}
}
}
void ClearGrid ()
{
foreach (Transform child in transform)
{
//if(child.name.Contains("Instance_"))
if(child.gameObject.name.Contains("Instance_"))
{
DestroyImmediate(child.gameObject);
}
}
}
// Update is called once per frame
void Update()
{
if (Application.IsPlaying(gameObject))
{
// Play logic
}
else
{
// Editor logic
ClearGrid();
CreateGrid();
}
}
}
OnValidate is simply called whenever one of the component's values is changed in the inspector. It's meant to be used for things like clamping a number between a $$anonymous$$ and a max value. So the intended way to use it is going through all the to-be-validated fields and doing something with each.
Doing something complex like instantiating or destroying a bunch of GameObjects means quite a bit of hacky extra work. At some point, learning how to write editor scripts might really be the more viable option.
Alternatively, not doing this in the editor but at runtime can also be considered.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Need help with fixing a code 1 Answer
Checkibg Rigidbody2d and component problems 0 Answers
Distribute terrain in zones 3 Answers
Enabling multiple Monobehaviour Components in a game object 1 Answer