- Home /
Error cs1922
Hey everybody! I'm been working on BurgzergArcade's Hack and Slash tutorial and now I'm receiving these errors
Assets/Scripts/Character Class/BaseCharacter.cs(115,82): error CS1922: A field or property ModifyingAttribute' cannot be initialized with a collection object initializer because type
ModifyingAttribute' does not implement `System.Collections.IEnumerable' interface
I get 12 of these and they all go to my BaseCharacter.cs and the lines they go to are my my setupskillmodifiers,
here is the script if anyone for anyone who needs it to help find the error
using UnityEngine;
using System.Collections;
using System; //added to access Enum class
public class BaseCharacter : MonoBehaviour {
private string _name;
private int _level;
private uint _freeExp;
private Attribute[] _primaryAttribute;
private Vital[] _vital;
private Skill[] _skill;
public void Awake() {
_name = string.Empty;
_level = 0;
_freeExp = 0;
_primaryAttribute = new Attribute[Enum.GetValues(typeof(AttributeName)).Length];
_vital = new Vital[Enum.GetValues(typeof(VitalName)).Length];
_skill = new Skill[Enum.GetValues(typeof(SkillName)).Length];
SetupPrimaryAttributes();
SetupVitals();
SetupSkills();
}
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
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();
}
//take the adg of all the players skills and asign that as the palyer level
public void CalculateLevel() {
}
private void SetupPrimaryAttributes() {
for(int cnt = 0; cnt < _primaryAttribute.Length; cnt++)
_primaryAttribute[cnt] = new Attribute();
}
private void SetupVitals() {
for(int cnt = 0; cnt < _vital.Length; cnt++)
_vital[cnt] = new Vital();
SetupVitalModifiers();
}
private void SetupSkills() {
for(int cnt = 0; cnt < _skill.Length; cnt++)
_skill[cnt] = new Skill();
SetupSkillModifiers();
}
public Attribute GetPrimaryAttribute(int index) {
return _primaryAttribute[index];
}
public Vital GetVital(int index) {
return _vital[index];
}
public Skill GetSkill(int index) {
return _skill[index];
}
private void SetupVitalModifiers() {
//health
ModifyingAttribute health = new ModifyingAttribute();
health.attribute = GetPrimaryAttribute((int)AttributeName.Might);
health.ratio = 0.5f;
GetVital((int)VitalName.Health).AddModifier(health);
//stamina
ModifyingAttribute Stamina = new ModifyingAttribute();
Stamina.attribute = GetPrimaryAttribute((int)AttributeName.Agility);
Stamina.ratio = 1;
GetVital((int)VitalName.Stamina).AddModifier(Stamina);
//mana
ModifyingAttribute Mana = new ModifyingAttribute();
Mana.attribute = GetPrimaryAttribute((int)AttributeName.Willpower);
Mana.ratio = 1;
GetVital((int)VitalName.Mana).AddModifier(Mana);
}
private void SetupSkillModifiers() {
//melee offence
GetSkill((int)SkillName.Melee_Offence).AddModifier(new ModifyingAttribute{GetPrimaryAttribute((int)AttributeName.Might), .33f});
GetSkill((int)SkillName.Melee_Offence).AddModifier(new ModifyingAttribute{GetPrimaryAttribute((int)AttributeName.Agility), .33f});
//melee defence
GetSkill((int)SkillName.Melee_Defence).AddModifier(new ModifyingAttribute{GetPrimaryAttribute((int)AttributeName.Might), .33f});
GetSkill((int)SkillName.Melee_Defence).AddModifier(new ModifyingAttribute{GetPrimaryAttribute((int)AttributeName.Speed), .33f});
//magic offence
GetSkill((int)SkillName.Magic_Offence).AddModifier(new ModifyingAttribute{GetPrimaryAttribute((int)AttributeName.Willpower), .33f});
GetSkill((int)SkillName.Magic_Offence).AddModifier(new ModifyingAttribute{GetPrimaryAttribute((int)AttributeName.Intelligence), .33f});
//magic defence
GetSkill((int)SkillName.Magic_Defence).AddModifier(new ModifyingAttribute{GetPrimaryAttribute((int)AttributeName.Concentration), .33f});
GetSkill((int)SkillName.Magic_Defence).AddModifier(new ModifyingAttribute{GetPrimaryAttribute((int)AttributeName.Charisma), .33f});
//ranged offence
GetSkill((int)SkillName.Ranged_Offence).AddModifier(new ModifyingAttribute{GetPrimaryAttribute((int)AttributeName.Speed), .33f});
GetSkill((int)SkillName.Ranged_Offence).AddModifier(new ModifyingAttribute{GetPrimaryAttribute((int)AttributeName.Agility), .33f});
//ranged defence
GetSkill((int)SkillName.Ranged_Defence).AddModifier(new ModifyingAttribute{GetPrimaryAttribute((int)AttributeName.Might), .33f});
GetSkill((int)SkillName.Ranged_Defence).AddModifier(new ModifyingAttribute{GetPrimaryAttribute((int)AttributeName.Speed), .33f});
}
public void StatUpdate() {
for(int cnt =0; cnt < _vital.Length; cnt++)
_vital[cnt].Update();
for(int cnt = 0;cnt < _skill.Length; cnt++)
_skill[cnt].Update();
}
}
$$anonymous$$odifyingAttribute looks like its defined elsewhere, if I recall that series it's somewhere in attributes or stat classes - look at those and check syntax
well I'm really new to C# so I'm a noob to this so I'll show you where my modifying attribute is located
private List<$$anonymous$$odifyingAttribute> _mods ; //A list of Attributes that modify this stat
private int _modValue; //The amount added to the baseValue
public $$anonymous$$odifiedStat() {
_mods = new List<$$anonymous$$odifyingAttribute>();
_modValue = 0;
}
public void Add$$anonymous$$odifier($$anonymous$$odifyingAttribute mod) {
_mods.Add(mod);
}
private void Calculate$$anonymous$$odValue() {
_modValue = 0;
if(_mods.Count > 0)
foreach($$anonymous$$odifyingAttribute att in _mods)
_modValue += (int)(att.attribute.AdjustedBaseValue * att.ratio);
}
public new int AdjustedBaseValue {
get{ return BaseValue + BuffValue + _modValue; }
}
public void Update() {
Calculate$$anonymous$$odValue();
}
}
public struct $$anonymous$$odifyingAttribute {
public Attribute attribute;
public float ratio;
Is there more to this script? If there isn't you are missing lines at the bottom of it.
Answer by ArkaneX · Aug 22, 2013 at 04:23 PM
You can't create ModifyingAttribute instance using syntax:
new ModifyingAttribute{GetPrimaryAttribute((int)AttributeName.Might), .33f}
Using {xxx, yyy} tells the compiler that some collection with two elements will be created. Instead you have to use:
new ModifyingAttribute
{
attribute = GetPrimaryAttribute((int)AttributeName.Might),
ratio = 0.33f
}
This way you create instance, with already initialized fields.
Btw - if you're using System namespace, then watch out for potential problems, because there is Attribute type defined in this namespace.
Yes you are right .Thak you first. But How can him do that like this with none error in that video. GetSkill((int)SkillName.$$anonymous$$elee_Defence).Add$$anonymous$$odifier(new $$anonymous$$odifyingAttribute(GetPrimaryAttribute((int)AttributeName.Speed), 0.33f));
Because new $$anonymous$$odifyingAttribute(x,y) is valid - it's a constructor taking 2 parameters.
Answer by TheTopSkull · Aug 22, 2013 at 04:47 PM
ok I'm sorted on those errors but now I only have 3
Assets/Scripts/Character Class/BaseCharacter.cs(170,1): error CS1525: Unexpected symbol }', expecting
)', or ,' Assets/Scripts/Character Class/BaseCharacter.cs(172,61): error CS0201: Only assignment, call, increment, decrement, and new object expressions can be used as a statement Assets/Scripts/Character Class/BaseCharacter.cs(172,68): error CS1525: Unexpected symbol
)', expecting `;'
Here is the new code:
public class BaseCharacter : MonoBehaviour {
private string _name;
private int _level;
private uint _freeExp;
private Attribute[] _primaryAttribute;
private Vital[] _vital;
private Skill[] _skill;
public void Awake() {
_name = string.Empty;
_level = 0;
_freeExp = 0;
_primaryAttribute = new Attribute[Enum.GetValues(typeof(AttributeName)).Length];
_vital = new Vital[Enum.GetValues(typeof(VitalName)).Length];
_skill = new Skill[Enum.GetValues(typeof(SkillName)).Length];
SetupPrimaryAttributes();
SetupVitals();
SetupSkills();
}
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
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();
}
//take the adg of all the players skills and asign that as the palyer level
public void CalculateLevel() {
}
private void SetupPrimaryAttributes() {
for(int cnt = 0; cnt < _primaryAttribute.Length; cnt++)
_primaryAttribute[cnt] = new Attribute();
}
private void SetupVitals() {
for(int cnt = 0; cnt < _vital.Length; cnt++)
_vital[cnt] = new Vital();
SetupVitalModifiers();
}
private void SetupSkills() {
for(int cnt = 0; cnt < _skill.Length; cnt++)
_skill[cnt] = new Skill();
SetupSkillModifiers();
}
public Attribute GetPrimaryAttribute(int index) {
return _primaryAttribute[index];
}
public Vital GetVital(int index) {
return _vital[index];
}
public Skill GetSkill(int index) {
return _skill[index];
}
private void SetupVitalModifiers() {
//health
ModifyingAttribute health = new ModifyingAttribute();
health.attribute = GetPrimaryAttribute((int)AttributeName.Might);
health.ratio = 0.5f;
GetVital((int)VitalName.Health).AddModifier(health);
//stamina
ModifyingAttribute Stamina = new ModifyingAttribute();
Stamina.attribute = GetPrimaryAttribute((int)AttributeName.Agility);
Stamina.ratio = 1;
GetVital((int)VitalName.Stamina).AddModifier(Stamina);
//mana
ModifyingAttribute Mana = new ModifyingAttribute();
Mana.attribute = GetPrimaryAttribute((int)AttributeName.Willpower);
Mana.ratio = 1;
GetVital((int)VitalName.Mana).AddModifier(Mana);
}
private void SetupSkillModifiers() {
//melee offence
((int)SkillName.Melee_Offence).AddModifier(new ModifyingAttribute{
attribute = GetPrimaryAttribute((int)AttributeName.Might),
ratio = 0.33f
}
((int)SkillName.Melee_Offence).AddModifier(new ModifyingAttribute{
attribute = GetPrimaryAttribute((int)AttributeName.Agility),
ratio = 0.33f
}
//melee defence
((int)SkillName.Melee_Defence).AddModifier(new ModifyingAttribute{
attribute = GetPrimaryAttribute((int)AttributeName.Might),
ratio = 0.33f
}
((int)SkillName.Melee_Defence).AddModifier(new ModifyingAttribute{
attribute = GetPrimaryAttribute((int)AttributeName.Speed),
ratio = 0.33f
}
//magic offence
((int)SkillName.Magic_Offence).AddModifier(new ModifyingAttribute{
attribute = GetPrimaryAttribute((int)AttributeName.Concentration),
ratio = 0.33f
}
//magic defence
((int)SkillName.Magic_Defence).AddModifier(new ModifyingAttribute{
attribute = GetPrimaryAttribute((int)AttributeName.Intelligence),
ratio = 0.33f
}
((int)SkillName.Magic_Defence).AddModifier(new ModifyingAttribute{
attribute = GetPrimaryAttribute((int)AttributeName.Charisma),
ratio = 0.33f
}
//ranged offence
((int)SkillName.Ranged_Offence).AddModifier(new ModifyingAttribute{
attribute = GetPrimaryAttribute((int)AttributeName.Agility),
ratio = 0.33f
}
((int)SkillName.Ranged_Offence).AddModifier(new ModifyingAttribute{
attribute = GetPrimaryAttribute((int)AttributeName.Agility),
ratio = 0.33f
}
//ranged defence
((int)SkillName.Ranged_Defence).AddModifier(new ModifyingAttribute{
attribute = GetPrimaryAttribute((int)AttributeName.Speed),
ratio = 0.33f
}
((int)SkillName.Ranged_Defence).AddModifier(new ModifyingAttribute{
attribute = GetPrimaryAttribute((int)AttributeName.Agility),
ratio = 0.33f
}
}
public void StatUpdate() {
for(int cnt = 0; cnt < _vital.Length; cnt++)
_vital[cnt].Update();
for(int cnt = 0;cnt < _skill.Length; cnt++)
_skill[cnt].Update();
}
}
@TheTopSkull - unfortunately your code has a lot more errors. $$anonymous$$ultiple parentheses and semicolons is missing, int type doesn't have AddProperty method (unless added as an extension), and possible a few more, which are covered by those I'm able to find.
Unity Answers is not an online compiler. You have to get a basic knowledge of C# - there are a lot of tutorials online, for example here or here.
Without this knowledge, you will quickly get frustrated and stop your adventure with Unity.
thanks ArkaneX hopefully next time you see me I'll know allot more about C# and unity!
Answer by getyour411 · Aug 22, 2013 at 05:10 PM
I've gone through that entire HackNSlash tut and Petey never leaves you with code with doesn't compile due to syntax or other errors, so I would not advise changing anything since you don't know what you are really changing. Put it back to how it was and look at his next video in the series, sometimes he hits his cutoff point in one vid but fixes it at the start of the next. Anything else is a typo somewhere.
Dude I've checked and the first basecharacter.cs I posted never worked and I have the code down to a tee, to only change I've actually made is the name of the attributes and thats about it
If you changed the names of the attributes, did you also change the names in the Enum and all other places where his names were referenced? I've seen a ton of feedback on his YouTube channel and it all comes down to typos, cAse errors, etc. Heck give him ~10 bucks and he'll give you every script
Check your $$anonymous$$odifiedStat.cs - the one you posted above is missing a few lines at the bottom.
Your answer
![](https://koobas.hobune.stream/wayback/20220613112739im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
A node in a childnode? 1 Answer
Unity BurgZergArcade Tutorial 20 errors. 1 Answer
CS8025 error 1 Answer
How do i fix a Compiler error 2 Answers
Sort Two Arrays 1 Answer