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 mmangual_83 · Dec 03, 2013 at 05:43 PM · c#monodevelopmono-behaviour

Issues using the 'new' keyword

I have a class where I am instantiating another class like so:

 public class MyClass : MonoBehaviour{
    MyOtherClass obj_otherClass = new MyOtherClass();
 }


But for some reason it spits out a warning that reads: 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 UnityEngine.MonoBehaviour:.ctor() BackgroundSurvey:.ctor() MyOtherClass:.ctor()

What do I need to do in order to fix this issue? Thank you in advance!

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

3 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by mmangual_83 · Dec 03, 2013 at 07:18 PM

I figured it out, at least for my issues:

Step 1: Declare your foreign class as you would normally:

 MyOtherClass obj_otherClas;

Step 2: Initialize it in either Awake() or Start()

 void Awake()
    {
        obj_otherClas = GameObject.FindGameObjectWithTag("MainCamera").GetComponent<MyOtherClass>();
    }

Step 3: you are now free to use the instance and call its methods where you can.

obj_otherClass.myMethod();

Comment
Add comment · Show 1 · 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 Spinnernicholas · Dec 03, 2013 at 07:29 PM 0
Share

That code doesn't create a new instance, it just locates one that already exists on a gameobject tagged "$$anonymous$$ainCamera". If the instance doesn't already exist, it will just return null.

avatar image
1

Answer by WilliamLeu · Dec 03, 2013 at 07:12 PM

You either add MonoBehaviours to a script via the inspector in the Unity editor, or use AddComponent() (obj_otherclass = gameObject.AddComponent() ). Then you put initialization code in either Awake() or Start() - depending on its dependencies.

You're not allowed to use constructors for MonoBehaviour derived classes because Unity creates and destroys these classes for its own reasons/purposes in the background. It also can't be created via constructor (especially a default one) because there's a restriction that every behavior has to be owned by a specified GameObject.

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
1

Answer by Spinnernicholas · Dec 03, 2013 at 07:25 PM

If your class is derived from MonoBehaviour, then you need to create it by adding it to a GameObject. To add it to the current object do this:

 MyOtherClass obj_otherClass = (MyOtherClass)gameObject.AddComponent("MyOtherClass");

If you don't want your class to be a Component, you can derive it from ScriptableObject and instantiate it like this:

 MyOtherClass obj_otherClass = (MyOtherClass)ScriptableObject.CreateInstance("MyOtherClass");

If you to instantiate it as you are, you can't derive it from GameObject or ScriptableObject.

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

18 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

Related Questions

how to debug in monodevelop 1 Answer

How to draw an arrow from fps contoller 1 Answer

MonoDevelop 4.0.1 won't recognize "UnityEngine" namespace 5 Answers

Mathf.pingpong 4 Answers

FPS Controller rotate between two points c# 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