- Home /
Why do Public Variables overwrite a Scripts Default Value
Hi all, I've been a bit frustrated trying to make some minor changes to my games object pricing system, I've found that because these values are public (which they need to be as they are accessed by a multitude of other scripts), the Default script value is now ignored and only the Inspector value is Read and Used by the game, I attempted to use the [HideInInspector] Attribute to try force unity to ignore the inspector values all together (as I rarely if ever use the inspector unless manually assigning a GameObject/Prefab/Script/IMG etc, which I always use [SerializeField] anyway, I will only ever use a 'Public' Variable if it is to be required access by other scripts) though this just made things more frustrating as it was still using the 'old' Inspector value that is now Hidden and un-modifiable (unless I un-hide it) rather than the newly changed Script Default Value...
So the question is.. How can I have a value/variable that is accessible by other scripts and either doesn't exist in the Inspector and or ignores the inspectors value all together - Because with a 'well organised' (possibly less efficient) setup project with hundreds if not thousands of empty objects being used as folders to organise the scene, becomes very frustrating to have to deep dive into just to change a few values that I always have open in a script anyway that is 1 click away rather than being a possible few hundred clicks away just to get to the scripts inspector... I do understand that I could simply add each and every single one of these values to the Start() function and force it to a value that differs from the inspector value.. though I've been taught and trying my best to follow a rule of DO NOT REPEAT YOURSELF.. So I am just trying to understand how I can use the scripts Default Set Value and ignore the Inspector all together as Having default set values that do not match up with the Inspectors Default Value is very frustrating and even though a Public default value is useless I find myself updating these useless values anyway so to stop the confusion of... "HEY why is that value 4.3 but the value the game is using is 5.1?... ohh right that f*****g inspector..".
Left of Image is Script - Right of Image is Inspector
The reason the 'iPrice' and 'iAvgPrice' are identical is because the iPrice is changing on Run-Time depending on market value and the Avg is used as a reference to the player of what the middle ground price is - So if these values are tweaked or modified to suit the economy it means I've got to do it twice (in the script for my own piece and mind where Id prefer it all to happen anyway) and in the inspector as the changes made in script do not effect the inspector.
Answer by Captain_Pineapple · Jul 15, 2021 at 01:07 PM
hey there,
there are multiple solutions to this issue. The possibly first and easiest: make the variable private and write some function like this:
public int getSomeInteger()
{
return thatValueYouWanToAccess;
}
Same can be done to access and set it.
Solution 2: use the [HideInInspector]
variable modification. Link to documentation.
Solution 3 (assuming you want to have it read only):
Use getters/setters:
public int someInteger {public get; private set;} = 12345;
As a general rule of thumb in my opinion:
Try to avoid using public variables as much as possible... As your project grows, so does the amount of variables. At some point there is just too many to choose from when you have some object somewhere. And for your particular example: That smells like you should use a List/Array for that...
Answer by rage_co · Jul 15, 2021 at 01:04 PM
Actually, inspector value always overrides the default script value unless it's the first time you defined it, or if you attach it to a new game object (monobehaviour scripts).....if you want to override it through script....you can define that value in the Start() method...and that should work...hope this helps