Search Unity

Question How to set unique field value on component added via RequireComponent?

Discussion in 'Editor & General Support' started by rchapman, Oct 20, 2022.

  1. rchapman

    rchapman

    Joined:
    Feb 13, 2014
    Posts:
    105
    This seems like it should be pretty easy, but I can't get it working. Essentially:

    Component A requires Component B.
    When Component B gets added, I want to set a field to a unique Id (just a string Guid).

    The first thing I've noticed is that RequireComponent seems to run on *every* asset in the project with Component A. This is fine, but seems to be contrary to the docs (see https://forum.unity.com/threads/when-does-requirecomponent-add-components.1298112/).

    I've tried setting the unique Id on the added component in both OnValidate and Reset. In this case the value gets set in the editor, but it's not actually persisted anywhere. When I submit the prefabs and scenes to Git and pull them into a different project, the Guid is different. I can see by looking at the .asset and .unity files that the component added by RequireComponent is never actually persisted anywhere. If I make any change to the component in the editor everything is persisted properly. Where is this state actually stored?

    I've been trying to figure out a more complex flow involving EditorApplication.update, but the PrefabUtility class is extremely challenging to understand.
     
  2. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    7,935
    Probably because you aren't dirtying the object. Ergo, Unity doesn't know you've changed some values. This is a requirement when changing values via code nearly 100% of the time, so you need to dirty the object with methods such as
    EditorUtility.SetDirty()
    .
     
  3. rchapman

    rchapman

    Joined:
    Feb 13, 2014
    Posts:
    105
    That does not seem to work in this flow. Specifically the problem is that the added component does not show up in the prefab file at all. I believe the problem is with how RequireComponent is applied to prefabs that do not have the required component on them.
     
  4. rchapman

    rchapman

    Joined:
    Feb 13, 2014
    Posts:
    105
    Note that this is only an issue when applying the update to old projects where Unity is doing this back-door RequireComponent work. Adding these components in the editor sets everything properly. If I could figure out how to detect that the required component is not actually present in the prefab file that would help because then I could run an editor routine to commit all of the changes. I just have no idea where the component information is actually stored and how to detect it.
     
  5. rchapman

    rchapman

    Joined:
    Feb 13, 2014
    Posts:
    105
    I realized the original question wasn't clear--this flow is actually working if I add Component A in the editor. Where it breaks is if I introduce the "RequireComponent" attribute to an existing project. In this flow the editor adds Component B to every prefab, but the added component and its fields are not persisted in the prefab .asset file. I would actually prefer if RequireComponent would not automatically add components to existing assets as the documentation states because then I would have a straightforward way to update the prefabs manually.