- Home /
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.
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");
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
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.
Your answer
Follow this Question
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