How to prevent the value in the Inspector from overriding the value in a script?

I am trying to create supernova explosion in my game. The damage and effective range of the explosion is determined at runtime by its mass defined in a public variable defined in a script. I have created two game objects for this kind of explosion. One for the supernova itself, which has a script containing the mass variable and this variable is assigned with a random value in the Start() method. Another object is the explosion, which also has a script calculating the damage range of the explosion. Both objects have a public GameObject variable referencing each other. My plan is to have the supernova instantiating an explosion object and destroy itself. The instantiated explosion uses the GameObject variable associated with the instantiating supernova to retrieve its mass value using GetComponent in its Start() method. Yet the explosion script could not retrieve the value correctly. The value retrieved is the value set in the Inspector for the supernova script, not the value assigned at runtime.

The script for the supernova:

 public GameObject Explosion;
    public int Mass;

	void Start () {
             Mass = Random.Range(10000,20000); //Value assigned at runtime

The script for the explosion:

public GameObject Supernova; //Reference to the instantiating supernova game object
  public int Mass;
	void Start () {
    Mass=Supernova.GetComponent<SupernovaControl>().Mass; //Only the value in the inspector is returned

I tried to alter the mass value in the Inspector for supernova. Its own mass variable would still be the value assigned in Start() method, but the value retrieved by explosion script would become the value in the Inspector. So I think the value in the Inspector has overridden the runtime value when retrieved from another script. Is it possible to prevent this from happening? I only want the runtime value to be retrieved.

As @txzeenath said, it could be the case that you’re grabbing the value before Start() has run. Start() runs right before the first update cycle for the object. You should use Awake() if you want something to be done right as the script is instantiated.

Setting the Explosion’s Mass in its Awake() method and grabbing it in the Supernova’s Start() method should yield the intended result.

i think both brazmogu & txzeenath are correct when they guess that you’re grabbing the value before Start() has executed.

for what it’s worth, another way to hide things from the inspector is to wrap them in setter/getters.


class SomeClass : MonoBehavior {
	private float theVal;

	public float TheVal {
		get {
			return theVal;

someOtherCode() {
	SomeClass foo;

public float value1 = 123;

You can either not make it public. Or mark it with [System.NonSerialized] which hides it from the inspector.

Also, when linking scripts that that, there’s no guarantee that one script is ready before the other, unless you use the script ordering in project settings to force it.

You may be trying to grab “mass” before the Start function has even ran. (depending exactly how it’s set up that is).