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 jpilon · Dec 30, 2013 at 09:05 PM · guiwindows

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?

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
0

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.

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

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


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