Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 azmundai · May 20, 2020 at 04:49 PM · variablepublic variableprivateconvertingchanging

When I make a public variable private, editor still uses previous public value

I changed a float value from a public variable to a private variable, but when I run the script, the value is stuck at 0 (which I assume was the initial value when I first created the script).

Even if I change the value before converting the variable to private, it's still always equal to 0.

If I keep it public, it works just fine ..

I've saved the scene before converting as well but that has no effect.

Anyone know what's going on here?

Comment
Add comment · Show 2
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 Hellium · May 20, 2020 at 06:58 PM 0
Share

0 is the default value of an int/float variable. Are you sure you assign a value to your variable?

avatar image azmundai Hellium · May 21, 2020 at 01:30 AM 0
Share

yea its assigned when declared, I also tried assigning it in start .. but it's still 0 in update ..

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by Bunny83 · May 20, 2020 at 07:09 PM

I'm not sure what you're asking here. If a variable is private and not marked with the SerializeField attribute it is not serialized at all. So the variable will have it's default value when a class instance is created which is 0 if no field initializer is defined. If a variable is not serialized its value will not be preserved between runs or at edit time. For example if I declare a variable like this:

 public float test = 42;

The variable will be initialized with 42 whenever the instance is created / recreated. After the object is created the value would be overwritten by the serialized value if one is available. Once you create an instance of that class the value 42 would be serialized and from that point on the value of the field initializer is pretty much ignored.


Now if we change the value in the inspector from 42 to say 5 Unity will serialize the value 5 and that's the value the variable will be initialized with when the instance is deserialized. If we change the variable from public to private the variable isn't serialized anymore. So when the object is recreated / deserialized it will again have the value 42.


So if you want a variable to be serialized, you have to either declare it public or add the SerializeField attribute. So either this:

 public float test;

or this:

 [SerializeField]
 private float test;

In both cases the variable will be serialized and be editable in the inspector. If you don't want the variable to be serialized / editable you have to either make the variable private or add the NonSerialized attribute

 private float test;

or

 [System.NonSerialized]
 public float test;

In this case the variable is not serialized at all and can only be used at runtime. It will have it's usual default value in the beginning (if none is specified in a field initializer it will be 0).


In some very rare and special cases you might want to use the combination

 [SerializeField, HideInInspector]

In which case the variable is serialized but will not show up in the inspector. That means you can not edit it through the normal means. This only makes sense in combination with some custom editor functionality which takes care of setting the value.

Comment
Add comment · Show 2 · 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 azmundai · May 21, 2020 at 12:05 AM 0
Share

thanks, thats a great explanation .. but for some reason when I change it, it sets itself to 0. I even tried removing the script and replacing it with a fresh version .. and it's still 0 .. its really odd. Ill try and recreate the whole object I guess?

I just noticed, the script doesn't have an active/deactive button next to it would that have anything to do with it?

I tried recreating it .. its still 0 .. and i read up on active/de-active .. thats not it.

avatar image dahiyabunty1 azmundai · May 21, 2020 at 05:22 AM 0
Share

if u have no unity method like start update in ur script.. then no option for enable disable

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

135 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 avatar image avatar image avatar image avatar image avatar image

Related Questions

Clicking on an Object to Make it the Variable Target 1 Answer

How can i auto assign Public value VirtualJoystick to Prefabs 0 Answers

access a var from another script 3 Answers

Need Help With My GAME! 3 Answers

Unity Giving Errors after adding "Private" 1 Answer


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