• 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
1
Question by DarkSlash · Aug 31, 2015 at 12:35 PM · c#classmonobehaviour

Classic Class in C#?

I want to have classic class in C#, like class Somet$$anonymous$$ng() { bla bla bla } and then in any script type Somet$$anonymous$$ng myObject = new Somet$$anonymous$$ng;

One t$$anonymous$$ng I did is making a new script but it starts with: using UnityEngine; using System.Collections;

public class Somet$$anonymous$$ng: MonoBehaviour

is t$$anonymous$$s mandatory for all Unity3d classes?

Also, when I put Somet$$anonymous$$ng myObject = new Somet$$anonymous$$ng it says in the console "You are trying to create a MonoBehaviour using the 'new' keyword. T$$anonymous$$s is not allowed. MonoBehaviours can only be added using AddComponent(). Alternatively, your script can inherit from ScriptableObject or no base class at all UnityEngine.MonoBehaviour:.ctor()"

So what is the correct way to do t$$anonymous$$s?

Comment
Add comment · Show 1
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 DarkSlash · Aug 31, 2015 at 01:20 PM 0
Share

Scribe your answer was the first to be right. Put it as an answer (not a comment) to vote it right!

3 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by Mikilo · Aug 31, 2015 at 12:40 PM

Of course you can create your own classes. And since 5.0 structs!

Unity does not allow you to create MonoBehaviour, you must use AddComponent as it is said in the console.

When you create a C# script from Unity, Unity will look at (On Windows) C:\Program Files\Unity\Editor\Data\Resources\ScriptTemplates\81-C# Script-NewBehaviourScript.cs.txt

It is the template of all new C# script. It just a placeholder, you can do what you want in it, hell yeah!

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
avatar image
0

Answer by DevPoG · Aug 31, 2015 at 01:08 PM

 public class Somet$$anonymous$$ng: MonoBehaviour {...}

Means your class Somet$$anonymous$$ng inherit from MonoBehaviour. If you want to make a simple class, just remove the inheritance by writing public class Somet$$anonymous$$ng {...}!

Remember : to have a class being a component to add to GameObjects, you need it to inherit from MonoBehaviour !

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
avatar image
0

Answer by Scribe · Aug 31, 2015 at 12:39 PM

No, just delete ': MonoBehaviour' and the libraries that you don't need, and you have a normal class.

When you create it you need brackets as you are actually calling the default constructor.

 Somet$$anonymous$$ng myObject = new Somet$$anonymous$$ng();
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.

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

32 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 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

You are trying to create a MonoBehaviour using the new keyword. This is not allowed 2 Answers

Access the GameObject a class is attached to 1 Answer

How to use MonoBehaviours inside namespaces. 3 Answers

Multiple Cars not working 1 Answer

"The class named is not derived from MonoBehaviour or ScriptableObject!", although class is derived from MonoBehaviour 2 Answers


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