How To Declare variables outside the functions

Hi all

I followed the very good burgzergarcade.com tutos and I meet a little probleme in video number 24.

Unity don’t tell there is an error but the code works only partially while the script is complete and even more since I went on to see if there was an answer to my problem later but unfortunately there was not.

health, energy, mana, melee, magic and ranged are still 0 vhen they should change according to the ratio I use.

I read several times and I found that A variable declared inside a function can not be used outside that function and is redeclared each time the function is called.

so, i need to declare variable ouside the funtion to keep the scope through the whole class.

but i dont no how and what and where to do that and show me how to correct it

so,I need help

thanks a lot

sorry for long scripts

Character Generator

using UnityEngine;
using System.Collections;
using System;
public class CharacterGenerator : MonoBehaviour {
private PlayerCharacter _toon;
private const int STARTING_POINTS = 350;
private const int MIN_STARTING_ATTRIBUTE_VALUE=50;
private const int STARTING_VALUE = 20;
private int pointsleft;

private const int OFFSET = 5;
private const int LINEHEIGHT = 20;
private const int STAT_LABELLENGTH = 100;
private const int STAT_VALUE_WIDTH = 30;
private const int STAT_BUTTON_WIDTH = 20;
private const int STAT_BUTTON_HEIGHT = 20;
// Use this for initialization

void Start () {
	_toon = new PlayerCharacter();
	pointsleft = STARTING_POINTS;
	_toon.Awake();

	for (int cnt=0; cnt < Enum.GetValues(typeof(AttributeName)).Length; cnt++) {
		_toon.GetPrimaryAttribute (cnt).BaseValue = MIN_STARTING_ATTRIBUTE_VALUE;
		pointsleft = (STARTING_VALUE + MIN_STARTING_ATTRIBUTE_VALUE);
	}
	_toon.StatUpdate();
}

// Update is called once per frame
void Update () {
	_toon.StatUpdate();
}

void OnGUI() {
	DisplayName();
	DisplayPointsLeft();
	DisplayAttributes();
	DisplayVitals();
	DisplaySkills();
}

private void DisplayName() {
	GUI.Label(new Rect(10,10,50,25),"Name:");
	_toon.Name=GUI.TextField(new Rect(65,10,100,25), _toon.Name);
}

private void DisplayAttributes() {
	for(int cnt=0; cnt < Enum.GetValues(typeof(AttributeName)).Length; cnt++) {
		GUI.Label(new Rect(OFFSET ,40+(cnt*LINEHEIGHT),STAT_LABELLENGTH,LINEHEIGHT), ((AttributeName)cnt).ToString());
		GUI.Label(new Rect(STAT_LABELLENGTH+OFFSET,40+(cnt*LINEHEIGHT),30,LINEHEIGHT), _toon.GetPrimaryAttribute(cnt).AdjustedBaseValue.ToString());
		
		if(GUI.Button(new Rect(OFFSET+STAT_LABELLENGTH+STAT_VALUE_WIDTH,40+(cnt*LINEHEIGHT),STAT_BUTTON_WIDTH,STAT_BUTTON_HEIGHT),"-")) {
			if(_toon.GetPrimaryAttribute(cnt).BaseValue > MIN_STARTING_ATTRIBUTE_VALUE) {
				_toon.GetPrimaryAttribute(cnt).BaseValue--;
				pointsleft++;
				_toon.StatUpdate();
			}
		}
		if(GUI.Button(new Rect(OFFSET+STAT_LABELLENGTH+STAT_VALUE_WIDTH+STAT_BUTTON_WIDTH,40+(cnt*LINEHEIGHT),STAT_BUTTON_WIDTH,STAT_BUTTON_HEIGHT),"+")) {
			if(pointsleft > 0) {
				_toon.GetPrimaryAttribute(cnt).BaseValue++;
				pointsleft--;
				_toon.StatUpdate();
			}
		}
		
	}
}

private void DisplayVitals() {
	for(int cnt=0; cnt < Enum.GetValues(typeof(VitalName)).Length; cnt++) {
	GUI.Label(new Rect(OFFSET,40+((cnt+7)*LINEHEIGHT),STAT_LABELLENGTH,LINEHEIGHT), ((VitalName)cnt).ToString());
		GUI.Label(new Rect(STAT_LABELLENGTH+OFFSET,40+((cnt+7)*LINEHEIGHT),STAT_VALUE_WIDTH,LINEHEIGHT), _toon.GetVital(cnt).AdjustedBaseValue.ToString());
		//Debug.Log("toon vitals: "+_toon.GetVital(cnt).AdjustedBaseValue);
	}
}

private void DisplaySkills() {
	for(int cnt=0; cnt < Enum.GetValues(typeof(SkillName)).Length; cnt++) {
		GUI.Label(new Rect(250,40+(cnt*LINEHEIGHT),STAT_LABELLENGTH+STAT_VALUE_WIDTH+STAT_BUTTON_WIDTH*2+OFFSET,LINEHEIGHT), ((SkillName)cnt).ToString());
		GUI.Label(new Rect(355,40+(cnt*LINEHEIGHT),STAT_LABELLENGTH+STAT_VALUE_WIDTH+STAT_BUTTON_WIDTH*2+OFFSET+STAT_LABELLENGTH,LINEHEIGHT), _toon.GetSkill(cnt).AdjustedBaseValue.ToString());
	}
	
}
private void DisplayPointsLeft() {
	GUI.Label(new Rect(250,10,STAT_LABELLENGTH,LINEHEIGHT),"Points Left: " + pointsleft.ToString());
}

}

BaseCharacter

using UnityEngine;
using System.Collections;
using System; //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();
	
}

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();
	}
	
}
private void SetupVitals(){
	for (int cnt = 0; cnt < _vital.Length; cnt++)
		_vital[cnt] = new Vital();
	SetupVitalModifiers(); //not seen on tutorials
	//Debug.Log("Vital: "+_vital[1].AdjustedBaseValue);
	
	SetupVitalModifiers();
}

private void SetupSkills(){
	for (int cnt = 0; cnt < _skill.Length; cnt++)
		_skill[cnt] = new Skill();
	SetupSkillModifiers(); //not seen on tutorials
	
	SetupSkillModifiers();
}

public Attribute GetPrimaryAttribute(int index) {
	//Debug.Log(index);
	return _primaryAttribute[index];
}

public Vital GetVital(int index) {
	return _vital[index];
}

public Skill GetSkill(int index) {
	//Debug.Log("Skill: "+_skill[index].Basevalue);
	return _skill[index];
}
private void SetupVitalModifiers() {
	//health
	ModifyingAttribute health = new ModifyingAttribute();
	health.attribute = GetPrimaryAttribute((int)AttributeName.Constituion);
	health.ratio = .5f;
	
	GetVital((int)VitalName.Health).AddModifier(health);
	
	GetVital((int)VitalName.Health).AddModifier(new ModifyingAttribute{attribute = GetPrimaryAttribute((int)AttributeName.Constituion), ratio = .5f});
	
	//energy
	
	ModifyingAttribute energyModifier = new ModifyingAttribute();
	energyModifier.attribute = GetPrimaryAttribute((int)AttributeName.Constituion);
	energyModifier.ratio = 1;
	
	GetVital((int)VitalName.Energy).AddModifier(energyModifier);
	
	//mana
	
	ModifyingAttribute manaModifier = new ModifyingAttribute();
	manaModifier.attribute = GetPrimaryAttribute((int)AttributeName.Willpower);
	manaModifier.ratio = 1;
	
	GetVital((int)VitalName.Mana).AddModifier(energyModifier);
}
private void SetupSkillModifiers() {
	// Melee Offence
	//GetSkill((int)Skill.Skillname.Melee_Offence).AddModifier(new ModifyingAttribute(GetPrimaryAttribute((int)Attribute.Attributename.Might), .33f));
	//GetSkill((int)Skill.Skillname.Melee_Offence).AddModifier(new ModifyingAttribute(GetPrimaryAttribute((int)Attribute.Attributename.Nimbleness), .33f));
	// Melee Defence
	//GetSkill((int)Skill.Skillname.Melee_Defence).AddModifier(new ModifyingAttribute(GetPrimaryAttribute((int)Attribute.Attributename.Speed), .33f));
	//GetSkill((int)Skill.Skillname.Melee_Defence).AddModifier(new ModifyingAttribute(GetPrimaryAttribute((int)Attribute.Attributename.Constitution), .33f));
	//Magic Offence
	//GetSkill((int)Skill.Skillname.Magic_Offence).AddModifier(new ModifyingAttribute(GetPrimaryAttribute((int)Attribute.Attributename.Concentration), .33f));
	//GetSkill((int)Skill.Skillname.Magic_Offence).AddModifier(new ModifyingAttribute(GetPrimaryAttribute((int)Attribute.Attributename.Willpower), .33f));
	//Magic Defence
	//GetSkill((int)Skill.Skillname.Magic_Defence).AddModifier(new ModifyingAttribute(GetPrimaryAttribute((int)Attribute.Attributename.Concentration), .33f));
	//GetSkill((int)Skill.Skillname.Magic_Defence).AddModifier(new ModifyingAttribute(GetPrimaryAttribute((int)Attribute.Attributename.Willpower), .33f));
	//Ranged Offence
	//GetSkill((int)Skill.Skillname.Ranged_Offence).AddModifier(new ModifyingAttribute(GetPrimaryAttribute((int)Attribute.Attributename.Concentration), .33f));
	//GetSkill((int)Skill.Skillname.Ranged_Offence).AddModifier(new ModifyingAttribute(GetPrimaryAttribute((int)Attribute.Attributename.Speed), .33f));
	//Ranged Defence
	//GetSkill((int)Skill.Skillname.Ranged_Defence).AddModifier(new ModifyingAttribute(GetPrimaryAttribute((int)Attribute.Attributename.Speed), .33f));
	//GetSkill((int)Skill.Skillname.Ranged_Defence).AddModifier(new ModifyingAttribute(GetPrimaryAttribute((int)Attribute.Attributename.Nimbleness), .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();
	
}

}

Hint: You are already doing so in lines 1-5 of the first code block. Except you are declaring them constants so they are not variables.

The variable private int pointsleft was declared in the very beginning of your script, outside any functions. So it should be accessible from anywhere in the class. Simply place your other variables there as well.

I think you are getting classes and functions confused. It is ok in the class. Even the private backing fields can be accessed using the properties from outside the class. You might want to read up on scope though.