- Home /
Annoying Warnings for Script cant figure it out
Heres the error code: "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() BaseStat:.ctor() ModifiedStat:.ctor() Skills:.ctor() BaseCharacter:SetupSkills() (at Assets/Game Data/Player Data/Scripts/Character Stats/BaseCharacter.cs:75) BaseCharacter:Awake() (at Assets/Game Data/Player Data/Scripts/Character Stats/BaseCharacter.cs:26) UnityEngine.Object:Instantiate(Object, Vector3, Quaternion) GameMaster:Start() (at Assets/Game Data/Player Data/Scripts/Character Stats/GameMaster.cs:17)"
Heres the script it affects. (Ignore the GameMaster and BaseCharacter:26 those are only affected because of BaseCharacter:75) IF you know whats wrong please help. Thanks..
Heres my script.
public class BaseCharacter : MonoBehaviour {
private string _name;
private int _level;
private uint _freeExp;
private Attribute[] _primaryAttribute;
private Vitals[] _vitals;
private Skills[] _skills;
public void Awake()
{
_name = string.Empty;
_level = 0;
_freeExp = 0;
_primaryAttribute = new Attribute[Enum.GetValues(typeof(AttributeName)).Length];
_vitals = new Vitals[Enum.GetValues(typeof(VitalName)).Length];
_skills = new Skills[Enum.GetValues(typeof(SkillName)).Length];
SetupPrimaryAttributes();
SetupVitals();
SetupSkills();
}
public string Name
{
get{ return _name;}
set{ _name = value;}
}
public int Level
{
get{ return _level;}
set{ _level = value;}
}
public uint FreeExp
{
get{ return _freeExp;}
set{ _freeExp = value;}
}
public void AddExp(uint exp)
{
_freeExp += exp;
CalculateLevel ();
}
public void CalculateLevel()
{
}
private void SetupPrimaryAttributes()
{
for(int cnt = 0; cnt < _primaryAttribute.Length; cnt++)
{
_primaryAttribute[cnt] = new Attribute();
_primaryAttribute[cnt].Name = ((AttributeName)cnt).ToString();
}
}
private void SetupVitals()
{
{
for(int cnt = 0; cnt < _vitals.Length; cnt++)
_vitals[cnt] = new Vitals();
}
SetupVitalModifiers();
}
private void SetupSkills()
{
{
for(int cnt = 0; cnt < _skills.Length; cnt++)
_skills[cnt] = new Skills();
}
SetupSkillModifiers();
}
public Attribute GetPrimaryAttributes(int index)
{
return _primaryAttribute[index];
}
public Vitals GetVitals(int index)
{
return _vitals[index];
}
public Skills GetSkills(int index)
{
return _skills[index];
}
private void SetupVitalModifiers()
{
//health
GetVitals((int)VitalName.Health).AddModifier(new ModifyingAttribute(GetPrimaryAttributes((int)AttributeName.Stamina), 10f));
GetVitals((int)VitalName.Health).AddModifier(new ModifyingAttribute(GetPrimaryAttributes((int)AttributeName.Endurance), 2f));
GetVitals((int)VitalName.Health).AddModifier(new ModifyingAttribute(GetPrimaryAttributes((int)AttributeName.Agility), .5f));
GetVitals((int)VitalName.Health).AddModifier(new ModifyingAttribute(GetPrimaryAttributes((int)AttributeName.Dexterity), .25f));
//power
GetVitals((int)VitalName.Power).AddModifier(new ModifyingAttribute(GetPrimaryAttributes((int)AttributeName.Intelligence), 10f));
GetVitals((int)VitalName.Power).AddModifier(new ModifyingAttribute(GetPrimaryAttributes((int)AttributeName.Wisdom), 2f));
GetVitals((int)VitalName.Power).AddModifier(new ModifyingAttribute(GetPrimaryAttributes((int)AttributeName.Charisma), 2f));
}
private void SetupSkillModifiers()
{
//melee
GetSkills((int)SkillName.Melee_Damage).AddModifier(new ModifyingAttribute(GetPrimaryAttributes((int)AttributeName.Strength), 2f));
GetSkills((int)SkillName.Melee_Defense).AddModifier(new ModifyingAttribute(GetPrimaryAttributes((int)AttributeName.Endurance), 1f));
GetSkills((int)SkillName.Melee_Defense).AddModifier(new ModifyingAttribute(GetPrimaryAttributes((int)AttributeName.Stamina), 1f));
//magic
GetSkills((int)SkillName.Magic_Damage).AddModifier(new ModifyingAttribute(GetPrimaryAttributes((int)AttributeName.Wisdom), 3f));
GetSkills((int)SkillName.Magic_Defense).AddModifier(new ModifyingAttribute(GetPrimaryAttributes((int)AttributeName.Intelligence), 1.5f));
//ranged
GetSkills((int)SkillName.Ranged_Damage).AddModifier(new ModifyingAttribute(GetPrimaryAttributes((int)AttributeName.Dexterity), 4f));
GetSkills((int)SkillName.Ranged_Defense).AddModifier(new ModifyingAttribute(GetPrimaryAttributes((int)AttributeName.Agility), 2.25f));
//healing
GetSkills((int)SkillName.Healing).AddModifier(new ModifyingAttribute(GetPrimaryAttributes((int)AttributeName.Charisma), 3.5f));
GetSkills((int)SkillName.Healing).AddModifier(new ModifyingAttribute(GetPrimaryAttributes((int)AttributeName.Intelligence),1.5f));
GetSkills((int)SkillName.Healing).AddModifier(new ModifyingAttribute(GetPrimaryAttributes((int)AttributeName.Wisdom), .5f));
}
public void StatUpdate()
{
for(int cnt = 0; cnt < _vitals.Length; cnt++)
_vitals[cnt].Update ();
for(int cnt = 0; cnt < _skills.Length; cnt++)
_skills[cnt].Update ();
}
}
Answer by orb · Sep 18, 2014 at 02:06 AM
The warning means exactly what it says: Either make the class derive from ScriptableObject instead of MonoBehaviour, or attach it to a game object via AddComponent() instead of calling new. If you're having one class for any attribute and the available ones might change during runtime, the ScriptableObject is your best bet. But if the attributes are set in stone at build time you might want to refactor it to either have per-attribute components (slightly clunky) or an Attributes component that holds all of them.
Uhm, ScriptableObjects can't be created with "new" either. You have to use CreateInstance ins$$anonymous$$d. I don't see a reason why the Attribute class should be derived from $$anonymous$$onoBehaviour or ScriptableObject at all.
Answer by chrismcrae5712 · Oct 06, 2014 at 12:29 AM
Woah woah woah guys calm down lol I was following a tutorial.
Answer by Cherno · Oct 06, 2014 at 12:41 AM
Orb gave you the explaination, I try to make it clearer to you.
To solve your problem, it's important to know how your custom classes (Vitals etc) are defined. They should either be in the same script that uses them (if that's the only script that uses this type of class), OR have each one's code in a seperate script that doesn't have the "uses MonoBehavior" part in the script's title. The latter one allows you to use your custom class in any other script.