How to edit component values on exposed properties?

I have a component called “Projectile.cs” and I would like to edit all the objects (both the prefabs and scene objects) that have this script attached to them.

For example, I would like to run this code on all these objects:

        var rb = GetComponent<Rigidbody>();
        if (rb != null)
        {
            rb.useGravity = false;
        }

I have been using OnValidate() before to do this kind of operations.
But it’s unclear to me why sometimes it doesn’t work on all my objects.

So… what’s a good way to edit: prefabs and scene objects? How do you do it? Or is it a bad idea to even do so, considering it might create changes on prefabs instances and make your prefab instance not update along with your prefab?
I’m open to learning anything about this subject, so let me know :slight_smile:

When I’m using OnValidate() to edit exposed properties, it’s important that I check that I’m not setting a value to the same value.

This way I don’t create a change on my prefab’s instances for nothing (because prefab instances keep track of those changes)

See this code below: I’m checking that useGravity is indeed not false before setting it to false.

var rb = _go.GetComponent<Rigidbody>();
if (rb != null && rb.useGravity != false)
            rb.useGravity = false;

Reference all the objects into an array then you can loop through them all at will.