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
5
Question by Jodll · Mar 27, 2012 at 11:23 AM · classaddcomponentconstructor

Class constructor

Hi,

This is a newbie question, but I really don't get how class constructors work.

I created a class, say "MyClass", and want to create a new object in another class, say "MyGame", and instantiate it with "MyClass". I code in C# and used the old "new MyClass(parameters)" constructor.

However, this is not allowed in Unity. I tried removing the "MonoBehaviour" inheritance from both classes, but it did not solve anything.

Besides, after reading the documentation, I tried creating a GameObject and using its AddComponent() method. However, I can't understand where I should pass the parameters to my constructor, and whether I should change the code inside "MyClass".

Thanks in advance.

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

1 Reply

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

Answer by Bunny83 · Mar 27, 2012 at 11:29 AM

Custom component, aka MonoBehaviour derived objects, can't have parameters in it's constructor and can't be created manually with new. Components are part of the strategy-design-pattern and can only be created with AddComponent()

For usual classes (not derived from MonoBehaviour) there's no problem creating them the usual way with new.

 class MyClass
 {
     public string name;
     public MyClass(string aName)
     {
         name = aName;
     }
 }
 
 MyClass mc = new MyClass("FooBar");

Comment
Add comment · Show 3 · 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 sebas77 · Mar 27, 2012 at 01:39 PM 0
Share

it is more the template method pattern http://en.wikipedia.org/wiki/Template_method_pattern since unluckily the Unity3D framework does not even know what an interface is

avatar image Bunny83 · Mar 27, 2012 at 02:00 PM 2
Share

I'm not sure what you mean. It's true that Unity doesn't use interfaces in their API, but there's not problem using them. However the $$anonymous$$onoBehaviour doesn't follow a template pattern. There are no abstract methods. Abstract methods as well as interfaces would force your to implement all specified methods.

$$anonymous$$eep in $$anonymous$$d that Unity is written in c++ and the C# callbacks are invoked from native code. They deter$$anonymous$$e what methods you have implemented via reflection and store this information along with your class. They have some kind of optimised call-lists. They invoke methods only on classes that have implemented this method.

However the question wasn't about how Unity invoke the methods ;)

It is for sure a strategy pattern. There is one container object (GameObject) and one "commone plugin type" (Component). Classes derived from Component can be "plugged in" a gameobject. Components can only live on GameObjects and therefore you can't create them manually. AddComponent will create the Component and add it to the GameObject.

As already said: they don't use a classical interface type. They actually do it in a way that isn't supported by the language, but since $$anonymous$$ono is "just" the scripting engine they can manipulate it from outside.

avatar image Jodll · Mar 28, 2012 at 09:47 AM 0
Share

Thanks! I used the same code as yours, and it works now.

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

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

add class or script to child gameObjects 1 Answer

Adding parameter to instance of a class during runtime 0 Answers

Using an enum in a class constructor. 1 Answer

Can i set class vars automatically when calling class constructor with attributes? 0 Answers

How do i edit a constructor array in the inspector? 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