- Home /
Creating class instances
I created a class for a drop down menu, and i tried making an instance of it in another script (the drop down menu class is not attatched to anything). However unity tells me that i can only use AddComponent() to add a monobehaviour. Does this mean that i really cant make instances of my classes? My class takes an x and y position, and a length and width inside its constructor. Example:
DropWindow dw = new DropWindow(100,125,150,50);
dw.dropDownWindow();
Is there any reason for your class to derive from $$anonymous$$onoBehaviour? If not, don't, then you can initialize it as usual. If it has to derive from $$anonymous$$onoBehaviour, then you must use AddComponent.
I think it does have to be because that class references images that i need.
Answer by Tct1856 · Aug 24, 2012 at 11:13 PM
You can, but only by using AddComponent, and not by calling constructor with new.
And your DropWindow should be derived from MonoBehavior.
it is derived from $$anonymous$$onoBehavior. Why cant i call a constructor with new? can you give me an example of how id do the same thing with AddComponent?
This was answered several times in the past, for ex., http://answers.unity3d.com/questions/280738/monobehaviour-new-keyword.html , you set the needed parameters afterwards ins$$anonymous$$d of passing them to constructor
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
FPS weapons class structure 1 Answer
Problem accessing enum from other class 1 Answer
Cannot get a class to show up correctly in a custom inspector 0 Answers