Component Public Default Values Problem

Does anyone else have this problem, or know a way around it?

  1. I make a component like this.

    public class ShipController : Monobehaviour {
    public float people = 10;
    public float guns = 3;
    }

  2. I have a object in my scene with this component.

  3. I run my game.

  4. I change the default values in ShipController.

  5. The changes do not appear in the scene. The values in the inspector take priority. My default values in ShipController are now worse than useless, they’re confusing.

What do other people do? Just never use public variables? Exposing them for debugging seems like a good idea but then bugs like this pop up.

Any default values for script parameters expressed in code will be applied only when the script is first added to a GameObject. Further changes to the default values will not be used by existing instances of your script component.

Once a script has been added the values of script parameters will be set by the inspector. In terms of debugging, if you change the values in the inspector while the game is running, then I would expect the changes to take affect. (Assuming the script is reading and acting on those values in some way.)

Additional Thought: If you only want to see/edit the values in the inspector once the game is running (rather than using it to set the initial values) then place your defaults in a Start() method instead…

	// this will appear in the inspector...
	public int MyValue;
	
	
	void Start()
	{
		// ...but then set my default value here.
		MyValue = 5;
	}

I change my values then simply save and reload my scene. Takes moments so doesn’t really impact the workflow.