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
2
Question by ina · Dec 24, 2014 at 10:08 PM · c#monobehaviourconstructoroop

C# Constructor in MonoBehaviour

How is the constructor called in MonoBehaviour?


It seems a constructor is called twice if it's in a gameobject via MonoBehaviour?

Example:

 using UnityEngine;
 using System.Collections;

 public class Constructors : MonoBehaviour {

 public Constructors(){
     print("Constructor");
    }
 }

When you press play in the editor, Console reads:

 Constructor
 UnityEngine.MonoBehaviour:print(Object)

 Constructor

When you press stop in the editor, Console has an additional extra line reading "Constructor" with these three separate lines:

 Constructor
 UnityEngine.MonoBehaviour:print(Object)

 Constructor

 Constructor 
Comment
Add comment · Show 4
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 Graham-Dunnett ♦♦ · Dec 24, 2014 at 10:29 PM 1
Share

Use Init() and Start() to do initialisation. Classes derived from $$anonymous$$onoBehaviour do not have constructors.

avatar image ina · Dec 25, 2014 at 05:57 AM 0
Share

What do you mean that classes derived from $$anonymous$$onoBehaviour do not have constructors? The constructor seems to be called anyway (?)

avatar image Kiwasi · Dec 25, 2014 at 06:30 AM 0
Share

This set up should give you a warning about constructors on monobehaviours. Either way you don't use them. As indicated do your initialisation in Awake and Start. Awake is typically for internal stuff and will be called immediatley. Start is for external stuff and gets called before the first frame of Update.

avatar image hav_ngs_ru · Dec 25, 2014 at 01:08 PM 0
Share

$$anonymous$$aybe you have two gameobjects with this script attached?

Speaking about constructors vs Awake/Start - actually you could use the first, but very carefully and only when you sure what exactly are you doing. For example to init static public Instance field for Singleton, or something like this. But keep in $$anonymous$$d that object with it's components, and all scene are not initialized at the coustructor execution moment, and even your public fields are not initialized by values set in inspector.

And as was said here - in 99.9% cases you should use Awake and Start.

1 Reply

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

Answer by Baste · Dec 25, 2014 at 02:25 PM

As everybody else is saying, don't use constructors. With the way unity works, you're supposed to use Awake and Start to handle initialization behavior.

To explain what you are seeing: yes, the constructor is called three times. You'd get the same result if you implemented the ISerializationCallbackReceiver interface, and put a debug in OnBefore/AfterDeserialize. You'd even get the same thing where the first debug would point back to the line of the debug, while the other one would be just the debug text with no extra info.

Now, I think that gives a clue about why you're recommended to not use constructors. See, serialization of the objects in the scene (and the scripts on them) happens a lot; whenever you save, whenever you press play, a lot of times. Instantiation of new objects, and thus calls to the constructor, probably happens as a part of that process. So if you say create a new gameobject as a part of your constructor, you'd be seeing that gameobject being added to your scene at different points when you didn't expect the constructor to be called.

Awake and Start, on the other hand, only happens exactly when the application starts playing, and when things are instantiated in play mode. So to prevent stuff from leaking from the serialization process to the scene, avoid constructors.

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 IgorAherne · Aug 25, 2017 at 05:09 AM 0
Share

I think this would be a good case to use a custom constructor hack http://answers.unity3d.com/answers/1398563/view.html

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

31 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

Related Questions

An OS design issue: File types associated with their appropriate programs 1 Answer

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

constructor called multiple times 1 Answer

why do these both codes work similarly when they are put inside update function... 3 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