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 /
This question was closed Jun 05, 2013 at 06:54 AM by Schnodahipfe for the following reason:

Problem is not reproducible or outdated

avatar image
0
Question by Schnodahipfe · Jun 04, 2013 at 03:04 PM · monobehaviourboolinitializationfield

bool field initialization ignored

Hi everyone,

I have two classes that derive from MonoBehaviour, something like this:

 public class A : MonoBehaviour {
     
     private B _b;
     
     private void SomeMethod()
     {
         _b = gameObject.AddComponent<B>();
     }
     
 }
 
 public class B : MonoBehaviour {
 
     private bool _foo = true;
     
     private void SomeMethod()
     {
         Debug.Log(_foo);
     }
     
 }

When The debug output in class B tells me that the bool field _foo is false, even though I initialized it with true in the head of the class, which is what I don't get. When I set _foo to true in some other method that is called after _b has been set with the AddComponent method, everything works as I expect.

Does this have something to do with the fact that _b is initialized with null? Does this result in all the field initializations in class B being ignored?

Thanks in advance!

Comment
Add comment · Show 5
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 robertbu · Jun 04, 2013 at 03:26 PM 0
Share

I think you simplified out the real issue with your sample code if in the "real" situation _foo is false. If B is added as a component either at edit time or at runtime _foo will be true.

avatar image TonyLi · Jun 04, 2013 at 03:47 PM 0
Share

I'm sure someone will suggest moving "_foo = true" to Awake(), which would certainly be one way to initialize it correctly.

But I'm curious why this isn't working. In what order are you calling A.Some$$anonymous$$ethod() and B.Some$$anonymous$$ethod()?

avatar image Schnodahipfe · Jun 04, 2013 at 04:35 PM 0
Share

B.Some$$anonymous$$ethod is called some time after A.Some$$anonymous$$ethod(). I think calling it the other way round would result in a NullPointerException. The strange thing is that _foo is not accessed anywhere else in my solution (according to the $$anonymous$$onoDevelop "Find references" and "Find in Files" functions). I also tried declaring it as "readonly" which didn't help either. Setting it in Awake() would probably work, but I am still curious why the above doesn't. It's the first declaration/initialization in the class.

avatar image TonyLi · Jun 05, 2013 at 02:46 AM 0
Share

I just tested this, and _foo was true. I used the sample code exactly as you posted it, except I changed both Some$$anonymous$$ethod() methods to public so I could call them from a control script.

Like robertbu wrote, the sample code probably simplified out the real issue.

avatar image Schnodahipfe · Jun 05, 2013 at 06:53 AM 0
Share

You guys were right. Somehow it works now, even though I don't have the slightest idea why, because I didn't change anything. $$anonymous$$aybe restarting the computer did the trick, or it was due to cosmic noise or the little men inside my computer who randomly change values of computer registers :D . Sorry to have bothered you with this!

0 Replies

  • Sort: 

Follow this Question

Answers Answers and Comments

15 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

Related Questions

Assign exposed vars before instantianting prefab? 3 Answers

Why child ScriptableObjects class instance cannot be placed in their parent ScriptableObject field in the inspector? 1 Answer

Initialize Fields when Created, not when Loaded 1 Answer

Writing a bool to a separate script without scripts name? 3 Answers

Toggle OnValueChanged ignore on start 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