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 I_Love_Misato · Nov 02, 2016 at 05:31 PM · variablesbug-perhapsmonobehaviourinitialization

Monobehaviour resets variables after the initialization cycle

I have a really weird problem. I've written a code that looks like this:

 UtilitiesList utilities; // Not derived from MonoBehaviour
 bool DEBUG_UtilitiesExists = false;
 
 void Start () {
 utilities = new UtilitiesList ();
 
 DEBUG_UtilitiesExists = utilities != null;
 Debug.Log ("DEBUG_Utilities exists: " + DEBUG_UtilitiesExists);
 
 //... Here I create utilities and add them to the UtilitiesList
 }
 
 void FixedUpdate () {
 
 if(utilities == null)
     Debug.Log ("DEBUG_Utilities exists: " + DEBUG_UtilitiesExists.ToString ());
 
 utilities.Update ();
 
 //... Further processing
 }

It works properly in 99 % of cases. But sometimes when I start the game it throws a NullRefernceException in the following string from FixedUpdate():

 utilities.Update ();

The most astonishing is that no exception is thrown from the Start() function! So initialization seems to be performed properly. And this is the only place where I assign a value to the "utilities" variable (and this variable is private). That makes me think that it doesn't depend on the script execution order; anyway, dependencies from the other monobehaviour components are not tight.

I added some variables for debugging as you can see above. And when it occures that the "utilities" variable becomes a null reference in FixedUpdate, "DEBUG_UtilitiesExists" variable is always true. That means that the initialization in Start is always performed successfully, but somehow it resets the reference somewhere between Start() and FixedUpdate().

The same situation occures with the other class which works with the XML files. The variable that is reset on its own, is a public property with a private set method.

I have absolutely no idea, what's happening)) Only that this is a Unity bug. If it is so, will it occur in the built game?

Comment
Add comment · Show 10
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 Glurth · Nov 02, 2016 at 07:03 PM 0
Share

As a sanity check, can you confirm that same occurs with a super-basic class, maybe even one without functions, rather than UtilitiesList?

avatar image I_Love_Misato Glurth · Nov 02, 2016 at 07:19 PM 0
Share

Super-basic class? What is it? I just have the other $$anonymous$$onoBehaviour-derived scripts which use the simple classes, and they never throw such exceptions.

avatar image Glurth I_Love_Misato · Nov 02, 2016 at 08:07 PM 0
Share

public class superBasic{ public int anInt; }

avatar image I_Love_Misato · Nov 02, 2016 at 07:49 PM 0
Share

By the way, I found out how to establish conditions to ivoke this problem, it will occure (for my project, of course) immediately rather than with a 1% probability as I denoted above. What I do: 1) Open "Script Edition Order" in inspector (Edit -> Project Settings -> Script Edition Order). 2) Place absolutely arbitrary class to the list of ordered scripts. alt text 3) And start the game WITHOUT pressing the "Apply Button". The messagebox, requiring to apply the settings will be shown, and I apply the settings in this messageBox. alt text Whenever I do this I always get this resetting variables problems. And I forgot to mention in the first place (I just thought this is obvious) that if I do nothing and just restart the game i editor there will be no problem again until a few days. Or (as I've just found out) until I change scripts execution order in any way. So I became more reassured that this is a Unity bug.

avatar image Glurth I_Love_Misato · Nov 02, 2016 at 08:11 PM 0
Share

Ok, so you are doing SO$$anonymous$$ETHING (hitting apply in the message box) between hitting start and the game actually running; Odd, but makes SO$$anonymous$$E sense. But if you don't do that, is there still a 1% chance it will happen?

avatar image I_Love_Misato Glurth · Nov 02, 2016 at 08:18 PM 0
Share

Yes, this problem is really-really rare if not to touch those exec order settings. Anyway, I'll report it as a bug and send them some of my project files. But if this is a problem occuring only in editor and it won't pass to the final built, I wouldn't warry about it, but I need to know for sure.

avatar image Dave-Carlile I_Love_Misato · Nov 02, 2016 at 08:12 PM 0
Share

Report it as a bug.

It's nice that it has a workaround though. Press apply before running your game :)

avatar image I_Love_Misato Dave-Carlile · Nov 02, 2016 at 08:21 PM 0
Share

))) I can't. Because it occures from time to time without me changing these settings)

avatar image Sergio7888 · Nov 02, 2016 at 08:38 PM 0
Share

Your UtilitiesList is Seriarizable?

avatar image I_Love_Misato Sergio7888 · Nov 03, 2016 at 04:26 PM 0
Share

No. Is it important?

0 Replies

· Add your reply
  • Sort: 

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

60 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

Related Questions

Assign exposed vars before instantianting prefab? 3 Answers

GameObject is null when assigning to non-monobehaviour public static class 1 Answer

PrefabUtility.ApplyPrefabInstance throws error when there is Script that does not directly derive on MonoBehaviour 0 Answers

Access variables,method from a Mono Behaviour without static use. -1 Answers

Public variable containing script 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