Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 shockawave123 · Aug 17, 2017 at 01:35 AM · objectprogrammingclass

Constructor called multiple times on Class

I have 2 classes. Class Alpha and Class Beta. Alpha is supposed to instantiate an instance of Beta, lets call that betaPieces. However, after a few frames, betaPieces is deleted out of nowhere and Alpha.betaPieces is equal to NULL What i discovered while debugging:

  • Constructor is called about 3 times.

  • Deconstructor is also called about 2 times. Some times in an unexpected order like Costructor, Constructor, Deconstructor, Constructor, Deconstructor

  • Triple checked if there are times when betaPieces is assigned, however it's never assigned anywhere else.

Below is a pretty stripped down version of the classes.

 public class Beta<IT,ID> where ID : IEquatable<ID>
 {

   private int windowSize;

   public Beta (int windowSize = 3)
   {
     this.windowSize = windowSize;
     //Called 3 times? Why?
     Debug.Log("Constructor Called");
   }

 }

 public class Alpha : MonoBehaviour
 {

   public Beta<GameObject, int> betaPieces;

   void Start(){
     betaPieces = new Beta<GameObject, int> ();
   }

   void Update(){
     //Null Exception After being called a few times
     Debug.Log(betaPieces.windowSize);
   }
 }

How do i stop Alpha.betaPieces from disappearing?

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 hexagonius · Aug 17, 2017 at 06:02 PM 0
Share

If I were you, I'd put a break point into your Start method. The rest of what you described only make sense if that was run multiple times, because everytime, betaPieces would be assigned a new object, leaving the previous for collection (destructor call). The last one would stay (as you said 3 constructor, 2 destructor calls).
Question is, why is Start ran 3 times, or betapieces created somewhere else too?

avatar image Bunny83 · Aug 17, 2017 at 06:16 PM 0
Share

You said "stripped down". How much did you strip? Is the class really a generic class? So it's not serialized in the inspector?

Your monobehaviour does not have the ExectueInEdit$$anonymous$$ode attribute?

Have you tried replacing your betaPieces variable by a property with an hidden field? Try adding debug logs to the setter and getter. $$anonymous$$ake sure you check the stack trace of each debug log. It usually tells you from where it was invoked

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Spydrouge · Jan 29, 2021 at 05:21 PM

It's hard to see exactly what you're doing here with the class pared down. Normally for this to happen, you would have had to do something in the Alpha constructor, or write [Serializable] above the Alpha Class, or be in a situation where a class who owns the Alpha class is being serialized. The behavior you're describing is a perfect match for a situation where Alpha is being serialized by Unity, and thus, as a result, it's refusing to let it's Beta variable be null. You mentioned you had destructors, and the destructors themselves could be getting called all will-nilly thanks to the deserialization. They very well might be your culprit.

Do not use constructors and destructors on or around Monobehaviors or [Serializable] classes unless you are prepared for them to get called over and over again. Monobehaviors/gameObjects are serialized by Unity, and Unity recreates and destroys objects all over the place every time it needs to serialize or deserialize something. Which is: Every time you save scripts for your project, every time you load your project, twice every time you play your project, every time you stop playing your project, and so on and so forth.

http://ilkinulas.github.io/development/unity/2016/05/30/monobehaviour-constructor.html

Hope tha thelps.

Comment
Add comment · 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

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

76 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

Related Questions

Multiple Cars not working 1 Answer

Return a value for a class? 1 Answer

Null reference.. 2 Answers

Making an item class in Unity. 1 Answer

Constructor giving nullreference error 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