• 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 Fred 2 · Nov 10, 2010 at 07:20 AM · monobehaviour

New monobehaviour warning.

I'm using new to create multiple monobehaviour class. It works where I can control many game objects with one script but I get this warning saying "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".

Here's my script.

var test1; test1 = new AnimationMoveNew(); test1.WaitAndMove("Cylinder.423");

Should I worry about the warning, or can I leave it because it builds and run without any problem.

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
6
Best Answer

Answer by duck · Nov 10, 2010 at 07:37 AM

Generally speaking this can cause problems, because certain parts of the Monobehaviour class rely on being connected to a valid GameObjects. If you don't happen to make use of those parts, you may not run into problems, but essentially - because it's not supported - the same rules as using undocumented functions apply: you run the risk that your code may act unpredictably, and the behaviour may change or break in future updates or versions of Unity.

The correct way is to to add your monobehaviour to an existing gameobject, like this:

(Javascript)

var thisInstance = someGameObject.AddComponent(MyScriptName);

or create a new empty gameobject and attach your monobehaviour instance to it, like this:

var newGO = new GameObject("new name");
var thisInstance = newGO.AddComponent(MyScriptName);

You can always destroy the temporary gameobject when you're finished with it, using Destroy()

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 Fred 2 · Nov 10, 2010 at 08:59 AM 0
Share

Cool, after adding all those scripts to the object the warning is gone. Thanks for the tips.

avatar image NeMewSys · Aug 19, 2012 at 07:32 PM 0
Share

What happens if "$$anonymous$$yScriptName" has some parameters to configure first (public/exposed vars)? How can you define values to it from AddComponent() method?

avatar image fafase · Jan 18, 2013 at 09:32 AM 0
Share

Thread long gone but I might as well answer the last bit. If you need to pass information like you would do in a constructor, you can either set them in the Start but that would mean they are kinda same for all instances.

If you need to give specific values then you can simply create a function like:

 function SetStartVariables(n:int){
     this.n = n;
 }

and then you do:

 var thisInstance = newGO.AddComponent($$anonymous$$yScriptName);
 thisInstance.SetStartVariables(value);

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

2 People are following this question.

avatar image avatar image

Related Questions

How to reference an EditorWindow script in a MonoBehaviour script OR how to pass variables from a EditorWindow to a MonoBehaviour script? 1 Answer

AudioClip in ScriptableObject 1 Answer

How do I use "helper classes" in C# and have them show up in the Inspector? 1 Answer

What we can achieve by putting MonoBehaviour inside namespaces? 1 Answer

link.xml for a monobehaviour? 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