Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
1 capture
11 Jun 22 - 11 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 Ezwrath · Jul 14, 2015 at 07:52 AM · newaddcomponentwarning

You are trying to create a MonoBehaviour using the 'new' keyword. This is not allowed. MonoBehaviours can only be added using AddComponent()

Hi guys, I've seen several posts before making one of the same warning "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()" But I still don't understand how to solve the problem. I understand I should use the addComponent method instead of "new" keyword, but I'm not sure how to do it in this context.

I have two scripts

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 public class ItemDatabase : MonoBehaviour {
 
     public List<Items> items;
 
     void Awake () {
         items = new List<Items>();
         items.Add(new Items(Resources.Load<Sprite>("Arcanum") as Sprite, "Arcane Staff", 0, 10, 0, Items.ItemType.Weapon));
         items.Add(new Items(Resources.Load<Sprite>("ArcaneOrb") as Sprite, "Arcane Orb", 1, 5, 0, Items.ItemType.Aura));
         items.Add(new Items(Resources.Load<Sprite>("Mystic Staff") as Sprite, "Arcane Orb", 2, 3, 0, Items.ItemType.Weapon));
     }
 }
 

And

 using UnityEngine;
 using System.Collections;
 
 public class Items : MonoBehaviour {
 
     public Sprite icon;
     public int id;
     public ItemType itemType;
 
     private string itemName;
     private int power;
     private int health;
 
     public Items(Sprite icon, string itemName, int id, int power, int health, ItemType type) {
         this.icon = icon;
         this.itemName = itemName;
         this.id = id;
         this.power = power;
         this.health = health;
         itemType = type;
     }
 
     public enum ItemType {
         Weapon, Aura, Head, Chest , Legs, Amulet, Ring
     }
 }

 

I get 3 of the same warnings mentioned above from the first script lines 11-13. How would I use addComponent in this scenario instead of the new keyword?

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 maccabbe · Jul 14, 2015 at 08:00 AM 1
Share

To add a monobehaviour you would use something like

gameObject.AddComponent();

http://docs.unity3d.com/ScriptReference/GameObject.AddComponent.html

However it doesn't seem like Items use any part of monobehaviours so you should probably just stop inheriting from it. Replace

 public class Items : $$anonymous$$onoBehaviour {

with

 public class Items {

and keep creating new Items objects using the new keyword.

1 Reply

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

Answer by tanoshimi · Jul 14, 2015 at 07:58 AM

Why does your Items class derive from Monobehaviour?

Change:

 public class Items : MonoBehaviour

to:

 public class Items
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 Ezwrath · Jul 14, 2015 at 12:50 PM 0
Share

That was my first solution before I made this post, but I got got an error from the gameObject I attached my Items Script too

references runtime script in scene file. Fixing!

Problem was without deriving $$anonymous$$onoBehaviour I would not be able to attach the script to a gameObject, but I guess in this case the script works fine without being attached to a gameObject, thanks!

avatar image hyzeae · May 16, 2017 at 07:13 AM 0
Share

but if I want to use API Instantiate ,how to deal with the class Items?

avatar image Bunny83 hyzeae · May 16, 2017 at 10:10 AM 0
Share

There are simply two cases:

  • Your script does derive from $$anonymous$$onoBehaviour, then you can attach the script to gameobjects but you can not create it with a class constructor but you have to use AddComponent on a gameobject if you want to create an instance from scripting.

  • Your script does not derive from $$anonymous$$onoBehaviour so it is not a component and can not be attached to a gameobject. However in this case you can (or have to) create the class with the class constructor and the new keyword.

The "Instantiate" method of the Unity API only works with components (i.e. classes derived from $$anonymous$$onoBehaviour) or other built-in classes of Unity. If you don't derive your class from $$anonymous$$onoBehaviour and you want to create a copy / duplicate of such an instance you have to either manually copy all the properties to a new instance or implement a copy constructor. A copy constructor is simply a constructor that takes it's own class type as parameter. In the case of the class that was mentioned in the OP it would look something like this:

 public Items(Items aSource)
 {
     icon = aSource.icon;
     id = aSource.id;
     itemType = aSource.itemType;
     itemName = aSource.itemName;
     power = aSource.power;
     health = aSource.health;
 }


With this additional constructor you can simply do:

 Items someItem;
 
 Items clone = new Items(someItem);

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

You are trying to create a MonoBehaviour using the 'new' keyword. 2 Answers

"MonoBehaviour with new" warning has no line number 2 Answers

When Unity3D finally supports WebGL? 2 Answers

Incrimenting a variable when in a certain area 1 Answer

error with learning script 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