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 chrismcrae5712 · Sep 18, 2014 at 01:47 AM · warningsannoying

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 ();
         }
 }
 

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
0

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.

Comment
Add comment · Show 2 · 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 Bunny83 · Sep 18, 2014 at 02:10 AM 0
Share

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.

avatar image orb · Sep 18, 2014 at 02:17 AM 0
Share

Yeah, I'm probably thinking of plain, old, regular classes.

avatar image
0

Answer by chrismcrae5712 · Oct 06, 2014 at 12:29 AM

Woah woah woah guys calm down lol I was following a tutorial.

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

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

25 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

Related Questions

Compilation failed because the compiler couldn't be executed! ? 1 Answer

Getting Unresolved Reference Warnings After Unity Update (To 5.3.4f1 64-bit) 3 Answers

error CS0649 1 Answer

Do I really have to do this? 2 Answers

Keyword 'void' cannot be used in this context 1 Answer


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