- Home /
unity gui.window in a class - monobehaviour error using new keyword
Hello,
I have a c# class that I reuse for creating simple GUI windows, here's a strip down version:
class Windows {
public Rect rect;
public bool Show;
public float Width;
public float Height;
public Vector2 start;
public Vector2 end;
public Windows ( float alpha, bool show, Vector2 size, Vector2 positionStart, Vector2 positionEnd){
this.rect = new Rect(positionStart.x, positionStart.y, size.x, size.y);
this.Alpha = alpha;
this.Show = show;
this.Width = size.x;
this.Height = size.y;
this.start = positionStart;
this.end = positionEnd;
}
// other functions
}
Now in some of my other scripts I reference it like this:
private Windows[] Window = new Windows[3];
void Start(){
Window[0] = new Windows (
1.0f,
true,
new Vector2(320,300), // width / height
new Vector2((Screen.width/2)-(320/2), (Screen.height)), // startup x / y
new Vector2((Screen.width/2)-(320/2), (Screen.height)-(300)) // end x / y
);
// more menus
}
void OnGUI(){
Window[0].rect = GUI.Window(0, Window[0].rect, DrawMenu1, "");
// more windows
}
void DrawMenu1(){
// gui buttons, etc
}
While it works 100% no runtime errors, unity is throwing some warnings:
You are trying to create a MonoBehaviour using the 'new' keyword. This is not allowed. MonoBehaviours can only be added using AddComponent(). Alternatively, your script can inherit from ScriptableObject or no base class at all
In my quest for error/warning free code, I can't figure out what do about this error. I'm still rather new to unity.
Can someone help me out?
Answer by Slobdell · Dec 30, 2013 at 09:21 PM
Your windows class, even though it isn't shown, probably inherits from monobehaviour. As it says, new isn't allowed. Just remove that part from the windows class, since you aren't doing anything that utilizes monobehaviour. Then you can do whatever you want with it since it's just a plain old C# clas.
Your answer
![](https://koobas.hobune.stream/wayback/20220613125155im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
GUIText multi-platform and multi-resolution position 1 Answer
How do i make my chat window save messages and be a scrolling GUI 0 Answers
Window isn't opening 1 Answer
Zoom effect in EditorGUI Windows 0 Answers
GUI: Windows inside Windows 1 Answer