- Home /
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?
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.
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
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!
but if I want to use API Instantiate ,how to deal with the class Items?
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);