#Change Mesh in editor when variable changed

1 messages · Page 1 of 1 (latest)

floral escarp
#

Hey there, got a quick question:
When I make an item, I want the mesh to change in the editor whenever I change the ScriptableObject that contains the item, for example, if I change the item to a medkit, it should turn into the mesh of a medkit. Anyone got any ideas of how I could do that in Vanilla Unity 2022?

half dome
#

You can use the OnValidate method. This gets invoked whenever a serialized variable is changed in the editor.

public class ItemHolder : MonoBehaviour
{
  [SerializeField] private Item item;

  private void OnValidate()
  {
    // First check if "item" is null.
    // If it is not, get MeshFilter component and apply item mesh.
    // Potentially repeat for MeshRenderer.
    // Or just spawn the Item's associated prefab as a child of the object (and replace existing children)
  }
}```
#

Note that OnValidate will only be called on MonoBehaviours and ScriptableObjects (even though Intellisense may not auto-complete it for the latter).

floral escarp
#

Thanks friend