Search Unity

Is there a way to update the value of other fields from a parent field in edit mode?

Discussion in 'Editor & General Support' started by FrickinSilly, Jun 9, 2022.

  1. FrickinSilly

    FrickinSilly

    Joined:
    Feb 19, 2020
    Posts:
    53
    Sorry for the poor phrasing. I'm not sure how to articulate it in a single sentence, but here's an example which will help demonstrate what I want.

    I have a Prefab of a parent game object Foo, with 5 child game objects. Each child has a sprite renderer on it. Depending on the level, I want to change the colour value of all 5 child game objects. Maybe in a firey level I want them all orange; in an ice level, blue.

    While I know I can highlight all of the game objects and change the colours at the same time, this is cumbersome when you have more complex objects, and if you add another child object to the prefab later on you have to go and update the value in all of the variants.

    So my next strategy is to create a script on the Foo parent with a publicly exposed "colour" field, and then on Awake/Start, find all child objects and set their SpriteRenderer colour value.

    This is better, but still cumbersome. I can only test this by going into Play mode.

    Is there a way for me to link this script to the Child colour values (or any fields of choice) at edit time?
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,745
    I think you can do it in editor mode too... I think you could just implement void OnValidate() and stick your code in there to send the color to all the child objects.
     
    FrickinSilly likes this.
  3. FrickinSilly

    FrickinSilly

    Joined:
    Feb 19, 2020
    Posts:
    53
    You have just blown my mind. This is EXACTLY what I was looking for. I had no idea OnValidate existed.


    Thank you!!!
     
    Kurt-Dekker likes this.