How can I make a variable editable in exactly one place in the inspector such that other scripts can read but not change that value?

I’m relatively new to all this, and while I feel like I’m grasping some of the concepts, I need to know the best practices.

I have three scripts, all dedicated to randomly generating asteroids and then destroying them when they’re out of range. They all have a variable for the radius around the player and a variable for the number of asteroids within that sphere, called density. I kept needing to change the variables in all three places, which was messy. I needed to be able to change all the variables at once in the inspector.

One thing I tried was (and again, this seemed like bad practice) was creating a parent class and making those three scripts extensions of that class. I declared the variables inside the parent class, and made them public so the children could see them. Any form of private or protected made them invisible. I then assigned the parent script to an empty game object so I could see it in the inspector, but then, since the variables were public, the other scripts had them in the inspector too. So then I still had to change the values everywhere.

I tried to make them private, but then use that [Serialize Field] thing, but then I couldn’t get the other scripts to read the variables. I tried to set up some sort of get function, but then the other scripts could still change the values in the inspector, so maybe I just botched that entirely.

I didn’t try any GetComponent stuff because the Unity tutorials said that it should only be used in a Start() function, since it’s taxing at run time. But maybe I misunderstood and that’s the way to go. Even so, I don’t know the syntax for what I want to do, or if it would fall into the same trap.

I’m moving in a different direction for making asteroids anyway, for other reasons. But there must be some way to do this, and my ignorance is killing me, and everything I’ve tried seems so messy.

So again: How can I make a variable editable in exactly one place in the inspector such that other scripts can read but not change that value?

First, to answer your main question, there’s really no magic trick to help you do it via a singe simple editor script or such. You have to for example make one “Tuner” GameObject to hold the data/values you want to tweak and give the other GameObjects another kind of script that gets the values from the Tuner object when they Awake(). Or make a the values you want to adjust static and make a Tuner object that sets the values of those static variables in its Awake().

One thing I tried was (and again, this seemed like bad practice) was creating a parent class and making those three scripts extensions of that class.

It seems to be a common misconception for people who are starting to learn about inheritance to think that it’s a way to share data among all objects that derive from that parent class. In reality it’s just a way to save yourself the trouble of declaring the same variables and methods over and over again when making a bunch of classes that have a lot in common.

If you make a class Animal and a classes Dog and Cat that extend it, and if Animal class has variables called int NumberOfLegs, bool SaysMeow and bool ClimbsTrees, it doesn’t mean that setting those variables in somewhere will make all Dogs meow and climb trees. Each instance of Animal (i.e. each Cat and Dog) still has their own values for those variables (as long as they are not static). But now when you want to make a new Animal e.g. Cow, you only need to write the Cow : Animal bit instead of having an empty class where you’d have to declare all 3 variables again.