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 cdytoby · Jun 01, 2015 at 02:19 PM · scripting problemeditor-scriptinglogconstructors

A weird issue in Unity related to scripts and Editor.

I have a class called PlayerData, marked as serializable, and it doesn't inherit any classes including MonoBehaviour. and it also doesn't contain any "InitializeOnLoad" things related to the Editor.

This constructor method contains a "Monobehaviour.Print("abc")" for example.

Now the issue happens, when I start the Unity, open this project, I didn't run anything, the constructor will be automatically run, because the "abc" will be automatically printed even the scene is not in running. When I see the stacktrace in Unity, it starts with this constructor, nothing else is calling it.

And, when I run the scene, this print() will be called multiple times, and some of them doesn't contain stacktraces in Unity, just a single line, which never happened before.

I don't have any other scripts which contains Editor or Editor related script.

What happened? I know it's related to the project, because it doesn't happen on other projects wich have the similar structure. I just want to know where to look into it.

I'm using Unity 5.0.2f1, this issue happened on both Windows and MacOS.

Comment
Add comment · Show 1
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 Vice_Versa · Jun 01, 2015 at 04:03 PM 0
Share

...it would help if you posted the code in here

1 Reply

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

Answer by Baste · Jun 01, 2015 at 04:19 PM

This happens because you have some serialized object which has a field of the PlayerData type.

Unity serialized and deserializes data A LOT. When you enter play mode, Unity serializes (saves to disk) the enitre scene you're in, starts the runtime, and then recreates everything from the serialized data - ie. deserializes it.

As a part of that process, the object has to get constructed. When it's constructed, the constructor is called and you see your print message.

Things that can cause Unity to serialize or deserialize data, and in the process call constructors, is for example:

  • going into play mode

  • exiting play mode

  • opening a scene in the editor

  • saving the scene/project

  • selecting an object in the editor, and viewing it in the inspector

  • a lot more!

Check out this blog post to read more informaiton about serialization, and the issues that might crop up.

To fix this, you might want to ditch the constructor, and move that code to some "Initialize()" function. Then you call that on Awake from whatever MonoBehaviour that contains this object. In general, [Serializeable] objects should follow the same rules that goes from MonoBehaviours - don't use constructors. This if the reason for that.

As an alternative, you could wrap all the stuff in the constructor you don't want to be happening on play in

 if(Application.isPlaying) {
     Monobehaviour.print("abc");
     ... //other runtime-only code
 }

That should take care of the messages, but the constructor will still be called.

Also, note that MonoBehaviour.print is the same thing as Debug.Log. MonoBehaviour.print exists so you can write

 print("hello world");

instead of:

 Debug.Log("hello world");

inside of a MonoBehaviour. If you're outside a MB, I think Debug.Log looks nicer.

Comment
Add comment · Show 1 · 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 cdytoby · Jun 01, 2015 at 04:44 PM 0
Share

Thanks! this helps.

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

22 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

Related Questions

On scene load function 0 Answers

Connecting oracle with unity tutorial, Steps or a way? 1 Answer

Get callback when assets loading or deserialization finished? 1 Answer

Reorderable list of UnityEvents 2 Answers

SRP URP Changing values of a material being shared by many objects in the Editor 0 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