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 maxnorth · Apr 04, 2015 at 03:26 AM · constructors

Puzzling Class Constructor Problem

The following two classes represent a pattern I've organized. The basics of it are that one class (let's call it MyClass) has a parameterless constructor that adds the instance of that class to a static MyClass array (which for purposes of simplification I have here included within MyClass as well). The second class is a MonoBehaviour that has member fields of MyClass, and they are initialized in their declaration with the custom default constructor. The problem I have is that when I have, let's say, 3 member MyClass fields, the length of the static MyClass array ends up equal to 6. No matter how many times I instantiate a MyClass, the array ends up twice as long.

HOWEVER, if I initialize the MyClass members with the same default constructor in a method body, such as Awake(), etc., the array ends up with the appropriate length. Now, it might seem like an obvious solution is to wait and initialize after the declaration, but the code is intended to be as user friendly as possible, so I really want the constructor to take care of everything. There seems to be no reason that this can't be done, so I'm hoping someone can give some insight into the problem.

Here's the code

 public class MyClass
 {
     public static MyClass[] staticArray;
     
     public void AddToStaticArray()
     {
         if (staticArray == null)
         staticArray = new MyClass[] {this} ;
         
         else
         {
             MyClass[] newArray = new MyClass[staticArray.Length + 1];
             for (int i = 0; i < staticArray.Length; i++)
             {
                 newArray[i] = staticArray[i];
             }
             newArray[staticArray.Length] = this;
             staticArray = newArray;
         }
     }
 
     public MyClass () {AddToStaticArray ();}
 }
 
 //Scenario 1
 public class MyMonoBehaviour : Monobehaviour
 {
     MyClass class1 = new MyClass();
     MyClass class2 = new MyClass(); 
 
     void Awake() {print (MyClass.staticArray.Length);} //output is 4
 }
 
 //Scenario 2
 public class AltMyMonoBehaviour : Monobehaviour
 {
     MyClass class1;
     MyClass class2;
 
     void Awake() 
     {
         class1 = new MyClass(); 
         class2 = new MyClass(); 
         print (MyClass.staticArray.Length);} //output is 2
     }
 }

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 maxnorth · Apr 04, 2015 at 04:02 AM 0
Share

Since posting I have done some debugging to deter$$anonymous$$e whether the declaration constructor is adding the same reference instance to the staticArray twice, or if each member of staticArray is its own instance. Strangely enough, there are no duplicates in the array. Every array member is a unique instance of $$anonymous$$yClass, which I suppose means that this declaration syntax is calling the constructor twice.

I would really appreciate anyone clearing this up for me.

1 Reply

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

Answer by maxnorth · Apr 08, 2015 at 04:11 PM

I figured out the problem, and it has to do with the way that the serializer works with default constructors. I was able to make the array have the correct length in two ways: 1) by marking each field with a NonSerializableAttribute (which was not ideal because at the very least I want users to be able to modify the class from the Inspector), and 2) by not having a default constructor with the AddToStaticArray() method. Even when I wasn't using the default constructor to initialize my fields, the serializer was calling it multiple times, apparently. So, since I wanted to keep serialization, I decided to forego the default constructor.

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

19 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

Related Questions

Constructors always returning void 2 Answers

Passing arguments into a constructor via AddComponent 5 Answers

How does one go about creating a list of GameObjects? 1 Answer

get_dataPath is not allowed to be called from a ScriptableObject constructor 0 Answers

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