Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by FuryFight3r · Jul 15, 2021 at 02:32 AM · variableinspectorhideignore

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..".

alt text

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.

screenshot-38.png (45.8 kB)
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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...

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
1

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

130 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Inspector remembers [HideInInspector] variables 2 Answers

Making a minor change to Default inspector? 1 Answer

Custom Inspector - How to add functionality? 1 Answer

Parent Class variables in Child Class's Inspector (C#) 0 Answers

How to assign script to inspector variable 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges