Need help with C# Script errors

Hey all I have been following the Burgzergarcade tutorials and have hit a dead end.

I am getting the following errors and can not see what is wrong to save my life. If you can help it would be appreciated very much. I’m new to the language and it doesn’t always make since to me.

ERRORS…

Assets/Scripts/Character Classes/ModifiedStat.cs(38,25): error CS0165: Use of unassigned local variable `temp’

Assets/Scripts/Character Classes/ModifiedStat.cs(32,23): error CS0161: `ModifiedStat.GetModifyingAttributesString()': not all code paths return a value

MY MODIFIEDSTAT.CS SCRIPT

using System.Collections.Generic;

public class ModifiedStat : BaseStat {
	private List<ModifyingAttribute> _mods;	//A list of Attribs that mod the stats
	private int _modValue;					// amount added to baseValue from mods
	
	public ModifiedStat() {
		_mods = new List<ModifyingAttribute>();
		_modValue = 0;
	}
	
	public void AddModifier(ModifyingAttribute mod) {
		_mods.Add(mod);
	}
	
	private void CalculateModValue() {
		_modValue = 0;
		
		if(_mods.Count > 0)
			foreach(ModifyingAttribute att in _mods)
				_modValue += (int)(att.attribute.AdjustedBaseValue * att.ratio);
	}
	
	public new int AdjustedBaseValue {
		get{ return BaseValue + BuffValue + _modValue;}
	}
	
	public void Update() {
		CalculateModValue();
	}
	
	public string GetModifyingAttributesString() {
		string temp;
		
//		UnityEngine.Debug.Log(_mods.Count);
		
		for(int cnt = 0; cnt < _mods.Count; cnt++) {
			temp += _mods[cnt].attribute.Name;
			temp += "_";
			temp += _mods[cnt].ratio;
			
			if(cnt < _mods.Count - 1)
				temp += "|";
		
      UnityEngine.Debug.Log(temp);
		return temp;
	}
}

public struct ModifyingAttribute {
	public Attribute attribute;
	public float ratio;
	
	public ModifyingAttribute(Attribute att, float rat) {
		attribute = att;
		ratio = rat;
		}
	} 
}

MY BASESTAT.CS

public class BaseStat {
	private int _baseValue;			// the base value of this stat
	private int _buffValue;			// the amount of buff to this stat
	private int _expToLevel;		// the total amount of exp to get to next level
	private float _levelModifier;	// the modifier applied to the exp needed to raise the skill
	
	public BaseStat() {
		_baseValue = 0;
		_buffValue = 0;
		_levelModifier = 1.1f;
		_expToLevel = 100;
	}
	
#region Basic Setters and Getters
	//Basic Setters and Gathers
	public int BaseValue {
		get{ return _baseValue; }
		set{ _baseValue = value; }
	}
	
	public int BuffValue {
		get{ return _buffValue; }
		set{ _buffValue = value; }
	}
	
	
	public int ExpToLevel {
		get{ return _expToLevel; }
		set{ _expToLevel = value; }
	}
	
	
	public float LevelModifier {
		get{ return _levelModifier; }
		set{ _levelModifier = value; }
	}
#endregion
	
	private int CalculateExpToLevel() {
		return (int)(_expToLevel * _levelModifier);
	}
	
	public void LevelUp() {
		_expToLevel = CalculateExpToLevel();
		_baseValue++;
	}
	
	public int AdjustedBaseValue {
		get{ return _baseValue + _buffValue;}
	}
}

If I need to post any other references let me know and thanks in advance.

Here’s an updated version of the GetModify… function

public string GetModifyingAttributesString() {
string temp = "";

//	 UnityEngine.Debug.Log(_mods.Count);

for(int cnt = 0; cnt < _mods.Count; cnt++) {
temp += _mods[cnt].attribute.Name;
temp += "_";
temp += _mods[cnt].ratio;

if(cnt < _mods.Count - 1)
temp += "|";


}
UnityEngine.Debug.Log(temp);
return temp;
}

lol ughhhhh. So frustrating that I missed that. However the second error still remains.

Assets/Scripts/Character Classes/ModifiedStat.cs(32,23): error CS0161: `ModifiedStat.GetModifyingAttributesString()': not all code paths return a value

and thank you very much for the reply. I have been searching for that error for 3 days

I tried to return is back a 0 in place off the _ but to no avail.

Make sure your brackets are in the correct place.

The code I posted should be all good. Did you replace the entire function, or just fix yours manually?

make sure return temp is the last thing that happens in the function (make sure its not inside the loop where you originally had it)

Oh, didn’t notice the loop either.

I copied yours in place, but I must have something very wrong. It popped up with 30 errors from my base character.cs

will have to go back a few tuts and see what I’m missing. Thanks for all the help.

Errors

Assets/Scripts/Character Classes/BaseCharacter.cs(90,65): error CS0246: The type or namespace name `ModifyingAttribute’ could not be found. Are you missing a using directive or an assembly reference?

Assets/Scripts/Character Classes/BaseCharacter.cs(90,49): error CS1502: The best overloaded method match for `ModifiedStat.AddModifier(ModifiedStat.ModifyingAttribute)’ has some invalid arguments

Assets/Scripts/Character Classes/BaseCharacter.cs(90,49): error CS1503: Argument #1' cannot convert object’ expression to type `ModifiedStat.ModifyingAttribute’

These same 3 reoccur every coded line from 90 to 107. You dont have to reply I will redo the tutoruials but just thought if you were board you could look.

using UnityEngine;
using System.Collections;
using System;				// to access the 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();
		
	}
	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 avg of all of the player's skills and assign that as the player level
	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 < _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
		GetVital((int)VitalName.Health).AddModifier(new ModifyingAttribute(GetPrimaryAttribute((int)AttributeName.Stamina), .5f));
		//energy
		GetVital((int)VitalName.Energy).AddModifier(new ModifyingAttribute(GetPrimaryAttribute((int)AttributeName.Intelligence), 1));
	}
	
	private void SetupSkillModifiers() {
		//melee offence	
		GetSkill((int)SkillName.Melee_Offence).AddModifier(new ModifyingAttribute(GetPrimaryAttribute((int)AttributeName.Strength), .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.Stamina), .33f));
		GetSkill((int)SkillName.Melee_Defence).AddModifier(new ModifyingAttribute(GetPrimaryAttribute((int)AttributeName.Agility), .33f));
		//HandGun offence	
		GetSkill((int)SkillName.Gun_Offence).AddModifier(new ModifyingAttribute(GetPrimaryAttribute((int)AttributeName.Focus), .33f));
		GetSkill((int)SkillName.Gun_Offence).AddModifier(new ModifyingAttribute(GetPrimaryAttribute((int)AttributeName.Agility), .33f));
		//melee offence	
		GetSkill((int)SkillName.Rifle_Offence).AddModifier(new ModifyingAttribute(GetPrimaryAttribute((int)AttributeName.Focus), .33f));
		GetSkill((int)SkillName.Rifle_Offence).AddModifier(new ModifyingAttribute(GetPrimaryAttribute((int)AttributeName.Agility), .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();
	}
}

Those 30 errors were probably just the next lot in line. (ie fixing those first two errors meant the compiler could get past that bit of code and into the next broken part)

Hard to say without all the code, but its telling you theres no such class as ModifyingAttribute. I see you have a struct defined in ModifiedStat.cs. I would probably change it to a class, and make sure its not inside another class…

Also, use the [ code ] [ /code ] tags around any code you post (remove extra spaces)

It was right there in my errors for me to read it lol. I put a space in

GetVital((int)VitalName.Health).AddModifier(new ModifyingAttribute(GetPrimaryAttribute((int)Attrib uteName.Stamina), .5f));

Attrib uteName.Stamina

AttributeName.Stamina.
Thanks for the replies

Hey Razial7 when you post code snippets could you please wrap it with the tag, or if you're in advanced posting, select your code and hit the # button. Thanks :)

Okay when I came home and tried to fix it that actual space did not exsist in the script somehow when I copied and pasted it that space appeared. I’m not entirely sure what you mean by me changing it to a class. Do i just add the class ModifyingAttribute as a class to the ModifiedStat.cs? I noticed that my BaseCharacter is pulling from MonoBehaviour is that correct?

The Errors:
Assets/Scripts/Character Classes/BaseCharacter.cs(89,65): error CS0246: The type or namespace name `ModifyingAttribute’ could not be found. Are you missing a using directive or an assembly reference?

Assets/Scripts/Character Classes/BaseCharacter.cs(89,49): error CS1502: The best overloaded method match for `ModifiedStat.AddModifier(ModifiedStat.ModifyingAttribute)’ has some invalid arguments

Assets/Scripts/Character Classes/BaseCharacter.cs(89,49): error CS1503: Argument #1' cannot convert object’ expression to type `ModifiedStat.ModifyingAttribute’

ModifiedStat Script

using System.Collections.Generic;

public class ModifiedStat : BaseStat {
	private List<ModifyingAttribute> _mods;	//A list of Attribs that mod the stats
	private int _modValue;					// amount added to baseValue from mods
	
	public ModifiedStat() {
		_mods = new List<ModifyingAttribute>();
		_modValue = 0;
	}
	
	public void AddModifier(ModifyingAttribute mod) {
		_mods.Add(mod);
	}
	
	private void CalculateModValue() {
		_modValue = 0;
		
		if(_mods.Count > 0)
			foreach(ModifyingAttribute att in _mods)
				_modValue += (int)(att.attribute.AdjustedBaseValue * att.ratio);
	}
	
	public new int AdjustedBaseValue {
		get{ return BaseValue + BuffValue + _modValue;}
	}
	
	public void Update() {
		CalculateModValue();
	}
	
	public string GetModifyingAttributesString() {
		string temp = "_";
		
//		UnityEngine.Debug.Log(_mods.Count);
		
		for(int cnt = 0; cnt < _mods.Count; cnt++) {
			temp += _mods[cnt].attribute.Name;
			temp += "0";
			temp += _mods[cnt].ratio;
			
			if(cnt < _mods.Count - 1)
				temp += "|";
		}
		
      UnityEngine.Debug.Log(temp);
		return temp;
	}


	public struct ModifyingAttribute {
		public Attribute attribute;
		public float ratio;
	
	public ModifyingAttribute(Attribute att, float rat) {
		attribute = att;
		ratio = rat;
		}
	}

BaseCharacter Script

using UnityEngine;
using System.Collections;
using System;				// to access the enum class

public class BaseCharacter : MonoBehaviour {
	private string _name;
	private int _level;				//mob and player level
	private uint _freeExp;			//mob and player exp to spend
	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();
		
	}
	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 avg of all of the player's skills and assign that as the player level
	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 < _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
		GetVital((int)VitalName.Health).AddModifier(new ModifyingAttribute(GetPrimaryAttribute((int)AttributeName.Stamina), .5f));
		//energy
		GetVital((int)VitalName.Energy).AddModifier(new ModifyingAttribute(GetPrimaryAttribute((int)AttributeName.Intelligence), 1));
	}
	
	private void SetupSkillModifiers() {
		//melee offence	
		GetSkill((int)SkillName.Melee_Offence).AddModifier(new ModifyingAttribute(GetPrimaryAttribute((int)AttributeName.Strength), .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.Stamina), .33f));
		GetSkill((int)SkillName.Melee_Defence).AddModifier(new ModifyingAttribute(GetPrimaryAttribute((int)AttributeName.Agility), .33f));
		//HandGun offence	
		GetSkill((int)SkillName.Gun_Offence).AddModifier(new ModifyingAttribute(GetPrimaryAttribute((int)AttributeName.Focus), .33f));
		GetSkill((int)SkillName.Gun_Offence).AddModifier(new ModifyingAttribute(GetPrimaryAttribute((int)AttributeName.Agility), .33f));
		//melee offence	
		GetSkill((int)SkillName.Rifle_Offence).AddModifier(new ModifyingAttribute(GetPrimaryAttribute((int)AttributeName.Focus), .33f));
		GetSkill((int)SkillName.Rifle_Offence).AddModifier(new ModifyingAttribute(GetPrimaryAttribute((int)AttributeName.Agility), .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();
	}
}

Attribute

public class Attribute : BaseStat {
	private string _name;
	
	public Attribute() {
		_name = "";
		ExpToLevel = 50;
		LevelModifier = 1.05f;
	}
	public string Name {
		get{ return _name;}
		set{ _name = value; }
	}
}

public enum AttributeName {
	Strength,
	Agility,
	Stamina,
	Intelligence,
	Willpower,
	Focus
}

Vital.cs

public class Vital : ModifiedStat {
	private int _curValue;
	
	public Vital() {
		_curValue = 0;
		ExpToLevel = 50;
		LevelModifier = 1.1f;
	}
	
	public int CurValue {
		get{
			if(_curValue > AdjustedBaseValue)
				_curValue = AdjustedBaseValue;
			
			return _curValue;
		}
		set{ _curValue = value; }
	}
}

public enum VitalName {
	Health,
	Energy
}

Skill.cs

public class Skill : ModifiedStat {
	private bool _known;
	
	public Skill() {
		_known = false;
		ExpToLevel = 25;
		LevelModifier = 1.1f;
	}
	
	public bool Known {
		get{ return _known; }
		set{ _known = value; }
	}
}

public enum SkillName {
	Melee_Offence,
	Melee_Defence,
	Gun_Offence,
	Rifle_Offence
}

^ Thank you :slight_smile: