#c# #visual-studio #unity3d
Вопрос:
Я объявил следующие переменные глобально.
economy_worth;
Covid_PT;
totalCost12;
vCountAmount;
Написали общедоступные функции AddCount() и AddPrice (), которые изменят значения totalCost12 и vCountAmount. Что на самом деле делает и показывает через Debug.Log().
С другой стороны, я написал еще одну публичную функцию PurchaseVents (), чтобы изменить значения economy_worth и Covid_PT, используя значения totalCost12 и vCountAmount. Но этот метод ничего не может изменить, так как он получает значение = ноль как для totalCost12, так и для vCountAmount. Кто-нибудь может сказать, почему эта функция получает нулевое значение для обеих переменных?
Значения vCountAmount и totalCost12 меняются, когда я нажимаю кнопку Плюс ( ). Debug.Log() обеих переменных после нажатия кнопки Плюс ( ), но когда я запускаю метод PurchaseVents (), он получает не обновленное значение vCountAmount и totalCost12, а ноль. Debug.Log() vCountAmount и totalCost12 в событиях покупок()
Полный код приведен ниже:
public class Game_Manager : MonoBehaviour
{ //COVID HOSPITAL
public Text CovidPT;
public Text CovidHealth;
public int Covid_PT;
public string covid_health;
//NATIONAL BANK
public Text economyWorth;
public Text economyHealth;
public int economy_worth;
public string economy_health;
//SOCIETY OFFICE / RESTRICTION
public Text restriction;
public string restrc;
//MARKET
public Text CovidBudget;
public int Covid_Budget;
//POPULATION/RESIDENTIAL AREA
public int population;
public Text pop;
public string ph;
public Text phealth;
//TAX COLLECTION
public int Tax_amount;
public Text TaxAmount;
//OVERALL PERFORMANCE
public string performance;
public Text performancehealth;
//VENTILATOR PURCHASE
public Text vCount;
public Text vCost;
public int vCountAmount;
public int totalCost12;
public int valueCopy;
//VACCINE PURCHASE
public Text vacCount;
public Text vacCost;
public int vacCountAmount;
public int totalCostVac;
//MASK PURCHASE
public Text maskCount;
public Text maskCost;
public int maskCountAmount;
public int totalCostMask;
//SHELTER PURCHASE
public Text shelterCount;
public Text shelterCost;
public int shelterCountAmount;
public int totalCostShelter;
//REWARDS
public int claim1st = 50000;
public int claim2nd = 30000;
public int claim3rd = 20000;
public int claim4th = 10000;
public int claim5th = 6000;
public int claim6th = 3000;
public int claim7th = 1500;
void Start()
{
//COVID PATEINTS
Covid_PT = 9000;
CovidPT.text = Covid_PT.ToString();
// CovidPT = GetComponent<Text>();
if (Covid_PT < 8000)
{
covid_health = "Good";
CovidHealth.text = covid_health.ToString();
// CovidHealth = GetComponent<Text>();
}
else
if (Covid_PT > 8100 || Covid_PT < 12100)
{
covid_health = "Bad";
CovidHealth.text = covid_health.ToString();
// CovidHealth = GetComponent<Text>();
}
else
if (Covid_PT > 12100)
{
covid_health = "Worst";
CovidHealth.text = covid_health.ToString();
// CovidHealth = GetComponent<Text>();
}
//NATIONAL BANK
// economy_worth = 900000;
economyWorth.text = economy_worth.ToString();
// economyWorth = GetComponent<Text>();
if (economy_worth > 1000000)
{
economy_health = "Excellent";
economyHealth.text = economy_health.ToString();
// economyHealth = GetComponent<Text>();
}
else
if (economy_worth > 800000)
{
economy_health = "Good";
economyHealth.text = economy_health.ToString();
// economyHealth = GetComponent<Text>();
}
else
if (economy_worth < 800000)
{
economy_health = "Bad";
economyHealth.text = economy_health.ToString();
// economyHealth = GetComponent<Text>();
}
//RESTRICTION
restrc = " Mild Lockdown";
restriction.text = restrc.ToString();
// restriction = GetComponent<Text>();
//MARKET
Covid_Budget = (int)(0.01 * economy_worth);
CovidBudget.text = Covid_Budget.ToString();
// CovidBudget = GetComponent<Text>();
//POPULATION / RESIDENTIAL AREA
population = 160000;
pop.text = population.ToString();
// pop = GetComponent<Text>();
if (population < 160000)
{
ph = "Bad Decrease";
phealth.text = ph.ToString();
// phealth = GetComponent<Text>();
}
else
if (population >= 160000)
{
ph = "Mild Decrease";
phealth.text = ph.ToString();
// phealth = GetComponent<Text>();
}
else
if (population >= 200000)
{
ph = "No Decrease";
phealth.text = ph.ToString();
// phealth = GetComponent<Text>();
}
//TAX COLLECTION
Tax_amount = (int)(0.8 * population);
TaxAmount.text = Tax_amount.ToString();
//TaxAmount = GetComponent<Text>();
//PERFORMANCE HEALTH
if (economy_health == "Good" amp;amp; covid_health == "Good" amp;amp; ph == "No Decrease")
{
performance = "Excellent";
performancehealth.text =
performance.ToString();
// performancehealth = GetComponent<Text>();
}
else
if (economy_health == "Good" amp;amp; covid_health == "Worst" amp;amp; ph == "Bad Decrease")
{
performance = "Worst";
performancehealth.text =
performance.ToString();
// performancehealth = GetComponent<Text>();
}
else
if (economy_health == "Good" amp;amp; covid_health == "Good" amp;amp; ph == "Mild Decrease")
{
performance = "Good";
performancehealth.text =
performance.ToString();
// performancehealth = GetComponent<Text>();
}
else
if (economy_health == "Good" amp;amp; covid_health == "Good" amp;amp; ph == "Bad Decrease")
{
performance = "Unsatisfactory";
performancehealth.text =
performance.ToString();
// performancehealth = GetComponent<Text>();
}
else
if (economy_health == "Good" amp;amp; covid_health == "Bad" amp;amp; ph == "No Decrease")
{
performance = "Satisfactroy";
performancehealth.text =
performance.ToString();
// performancehealth = GetComponent<Text>();
}
else
if (economy_health == "Good" amp;amp; covid_health == "Bad" amp;amp; ph == "Mild Decrease")
{
performance = "Unsatisfactory";
performancehealth.text =
performance.ToString();
// performancehealth = GetComponent<Text>();
}
else
if (economy_health == "Good" amp;amp; covid_health == "Bad" amp;amp; ph == "Bad Decrease")
{
performance = "Bad";
performancehealth.text =
performance.ToString();
// performancehealth = GetComponent<Text>();
}
else
if (economy_health == "Good" amp;amp; covid_health == "Worst" amp;amp; ph == "No Decrease")
{
performance = "Satisfactory";
performancehealth.text =
performance.ToString();
// performancehealth = GetComponent<Text>();
}
else
if (economy_health == "Good" amp;amp; covid_health == "Worst" amp;amp; ph == "Mild Decrease")
{
performance = "Unsatisfactory";
performancehealth.text =
performance.ToString();
// performancehealth = GetComponent<Text>();
}
else
if (economy_health == "Good" amp;amp; covid_health == "Worst" amp;amp; ph == "Bad Decrease")
{
performance = "Bad";
performancehealth.text =
performance.ToString();
// performancehealth = GetComponent<Text>();
}
else
if (economy_health == "Bad" amp;amp; covid_health == "Good" amp;amp; ph == "No Decrease")
{
performance = "Good";
performancehealth.text =
performance.ToString();
// performancehealth = GetComponent<Text>();
}
else
if (economy_health == "Bad" amp;amp; covid_health == "Good" amp;amp; ph == "Mild Decrease")
{
performance = "Satisfactory";
performancehealth.text =
performance.ToString();
// performancehealth = GetComponent<Text>();
}
else
if (economy_health == "Bad" amp;amp; covid_health == "Good" amp;amp; ph == "Bad Decrease")
{
performance = "Bad";
performancehealth.text =
performance.ToString();
// performancehealth = GetComponent<Text>();
}
else
if (economy_health == "Bad" amp;amp; covid_health == "Bad" amp;amp; ph == "No Decrease")
{
performance = "Unsatisfactory";
performancehealth.text =
performance.ToString();
// performancehealth = GetComponent<Text>();
}
else
if (economy_health == "Bad" amp;amp; covid_health == "Bad" amp;amp; ph == "Mild Decrease")
{
performance = "Unsatisfactory";
performancehealth.text =
performance.ToString();
// performancehealth = GetComponent<Text>();
}
else
if (economy_health == "Bad" amp;amp; covid_health == "Bad" amp;amp; ph == "Bad Decrease")
{
performance = "Bad";
performancehealth.text =
performance.ToString();
// performancehealth = GetComponent<Text>();
}
else
if (economy_health == "Bad" amp;amp; covid_health == "Bad" amp;amp; ph == "No Decrease")
{
performance = "Unsatisfactory";
performancehealth.text =
performance.ToString();
// performancehealth = GetComponent<Text>();
}
else
if (economy_health == "Bad" amp;amp; covid_health == "Bad" amp;amp; ph == "Mild Decrease")
{
performance = "Unsatisfactory";
performancehealth.text =
performance.ToString();
// performancehealth = GetComponent<Text>();
}
else
if (economy_health == "Bad" amp;amp; covid_health == "Bad" amp;amp; ph == "Bad Decrease")
{
performance = "Bad";
performancehealth.text =
performance.ToString();
// performancehealth = GetComponent<Text>();
}
else
if (economy_health == "Bad" amp;amp; covid_health == "Worst" amp;amp; ph == "No Decrease")
{
performance = "Bad";
performancehealth.text =
performance.ToString();
// performancehealth = GetComponent<Text>();
}
else
if (economy_health == "Bad" amp;amp; covid_health == "Worst" amp;amp; ph == "Mild Decrease")
{
performance = "Worst";
performancehealth.text =
performance.ToString();
// performancehealth = GetComponent<Text>();
}
else
if (economy_health == "Bad" amp;amp; covid_health == "Worst" amp;amp; ph == "Bad Decrease")
{
performance = "Worst";
performancehealth.text =
performance.ToString();
// performancehealth = GetComponent<Text>();
}
else
if (economy_health == "Worst" amp;amp; covid_health == "Good" amp;amp; ph == "No Decrease")
{
performance = "Unsatisfactory";
performancehealth.text =
performance.ToString();
// performancehealth = GetComponent<Text>();
}
else
if (economy_health == "Worst" amp;amp; covid_health == "Good" amp;amp; ph == "Mild Decrease")
{
performance = "Unsatisfactory";
performancehealth.text =
performance.ToString();
// performancehealth = GetComponent<Text>();
}
else
if (economy_health == "Worst" amp;amp; covid_health == "Good" amp;amp; ph == "Bad Decrease")
{
performance = "Bad";
performancehealth.text =
performance.ToString();
// performancehealth = GetComponent<Text>();
}
else
if (economy_health == "Worst" amp;amp; covid_health == "Bad" amp;amp; ph == "No Decrease")
{
performance = "Bad";
performancehealth.text =
performance.ToString();
// performancehealth = GetComponent<Text>();
}
else
if (economy_health == "Worst" amp;amp; covid_health == "Bad" amp;amp; ph == "Mild Decrease")
{
performance = "Bad";
performancehealth.text =
performance.ToString();
// performancehealth = GetComponent<Text>();
}
else
if (economy_health == "Worst" amp;amp; covid_health == "Bad" amp;amp; ph == "Bad Decrease")
{
performance = "Worst";
performancehealth.text =
performance.ToString();
// performancehealth = GetComponent<Text>();
}
else
if (economy_health == "Worst" amp;amp; covid_health == "Worst" amp;amp; ph == "No Decrease")
{
performance = "Bad";
performancehealth.text =
performance.ToString();
// performancehealth = GetComponent<Text>();
}
else
if (economy_health == "Worst" amp;amp; covid_health == "Worst" amp;amp; ph == "Mild Decrease")
{
performance = "Worst";
performancehealth.text =
performance.ToString();
// performancehealth = GetComponent<Text>();
}
else
if (economy_health == "Worst" amp;amp; covid_health == "Bad" amp;amp; ph == "Bad Decrease")
{
performance = "Worst";
performancehealth.text =
performance.ToString();
// performancehealth = GetComponent<Text>();
}
else
{
performance = "Not Sure";
performance.ToString();
// performancehealth = GetComponent<Text>();
}
//VENTILATOR PURCHASE
// vCount = GetComponent<Text>();
// vCost = GetComponent<Text>();
//VACCINE PURCHASE
// vacCount = GetComponent<Text>();
// vacCost = GetComponent<Text>();
//MASK PURCHASE
// maskCount = GetComponent<Text>();
// maskCost = GetComponent<Text>();
//SHELTER PURCHASE
// shelterCount = GetComponent<Text>();
// shelterCost = GetComponent<Text>();
}
// M E T H O D S
//VENTILATOR PURCHASE
public void AddCount()
{
vCountAmount ;
vCount = GetComponent<Text>();
vCount.text = vCountAmount.ToString();
Debug.Log("Num of vents: " vCountAmount);
}
public void AddPrice()
{
//valueCopy = 1800;
totalCost12 = 1800;
vCost = GetComponent<Text>();
vCost.text = totalCost12.ToString();
Debug.Log("total cost is: " totalCost12);
}
public void SubtractCount()
{
if (vCountAmount >= 1)
{
vCountAmount -= 1;
vCount = GetComponent<Text>();
vCount.text = vCountAmount.ToString();
Debug.Log(vCountAmount);
}
else
{
vCountAmount = 0;
vCount = GetComponent<Text>();
vCount.text = vCountAmount.ToString();
}
}
public void SubtractPrice()
{
Debug.Log("subtraction is occuring");
if (totalCost12 >= 1800)
{
totalCost12 -= 1800;
vCost = GetComponent<Text>();
vCost.text = totalCost12.ToString();
Debug.Log(totalCost12);
}
else
{
Debug.Log("majamaja");
totalCost12 = 0;
vCost = GetComponent<Text>();
vCost.text = totalCost12.ToString();
}
}
public void PurchaseVent()
{
// Debug.Log("value copy" valueCopy);
Debug.Log("before logic total cost is " totalCost12);
economy_worth -= totalCost12;
Debug.Log("Economy after purchase" economy_worth);
Debug.Log("total cost is " totalCost12);
Covid_PT -= vCountAmount * 60;
Debug.Log("Pateints after purchase" Covid_PT);
Debug.Log("Num of vents: " vCountAmount);
}
//VACCINE PURCHASE
public void AddCountVac()
{
vacCountAmount = 4;
vacCount = GetComponent<Text>();
vacCount.text = vacCountAmount.ToString();
Debug.Log("Num of vaccines: " vacCountAmount);
}
public void AddVaccine()
{
totalCostVac = 80 * 4;
vacCost = GetComponent<Text>();
vacCost.text = totalCostVac.ToString();
Debug.Log("total cost is " totalCostVac);
}
public void SubtractCountVac()
{
if (vacCountAmount >= 4)
{
vacCountAmount -= 4;
vacCount = GetComponent<Text>();
vacCount.text = vacCountAmount.ToString();
Debug.Log("Num of vaccines: " vacCountAmount);
}
else
{
vacCountAmount = 0;
vacCount = GetComponent<Text>();
vacCount.text = vacCountAmount.ToString();
}
}
public void SubtractVaccine()
{
if (totalCostVac >= 80 * 4)
{
totalCostVac -= 80 * 4;
vacCost = GetComponent<Text>();
vacCost.text = totalCostVac.ToString();
Debug.Log("total cost is " totalCostVac);
}
else
{
totalCostVac = 0;
vacCost = GetComponent<Text>();
vacCost.text = totalCostVac.ToString();
}
}
public void PurchaceVaccines()
{
Debug.Log("before logic total cost is " totalCostVac);
economy_worth -= totalCostVac;
Debug.Log(economy_worth);
Debug.Log("total cost is " totalCostVac);
Covid_PT -= vacCountAmount * 6 / 4;
Debug.Log(Covid_PT);
Debug.Log("Num of vaccines: " vacCountAmount);
}
//MASK PURCHASE
public void AddCountMask()
{
maskCountAmount = 50;
maskCount = GetComponent<Text>();
maskCount.text = maskCountAmount.ToString();
Debug.Log(maskCountAmount);
}
public void Addmask()
{
totalCostMask = 5 * 50;
maskCost = GetComponent<Text>();
maskCost.text = totalCostMask.ToString();
Debug.Log(totalCostMask);
}
public void SubtractCountMask()
{
if (maskCountAmount >= 50)
{
maskCountAmount -= 50;
maskCount = GetComponent<Text>();
maskCount.text = maskCountAmount.ToString();
Debug.Log(maskCountAmount);
}
else
{
maskCountAmount = 0;
maskCount = GetComponent<Text>();
maskCount.text = maskCountAmount.ToString();
}
}
public void Subtractmask()
{
if (totalCostMask >= 5 * 50)
{
totalCostMask -= 5 * 50;
maskCost = GetComponent<Text>();
maskCost.text = totalCostMask.ToString();
Debug.Log(totalCostMask);
}
else
{
totalCostMask = 0;
maskCost = GetComponent<Text>();
maskCost.text = totalCostMask.ToString();
}
}
public void PurchaceMasks()
{
Debug.Log("before logic total cost is " totalCostMask);
economy_worth -= totalCostMask;
Debug.Log(economy_worth);
Debug.Log("total cost is " totalCostMask);
Covid_PT -= maskCountAmount * 2 / 50;
Debug.Log(Covid_PT);
Debug.Log("Num of vaccines: " maskCountAmount);
}
//SHELTER PURCHASE
public void AddCountShelter()
{
shelterCountAmount ;
shelterCount = GetComponent<Text>();
shelterCount.text = shelterCountAmount.ToString();
Debug.Log(shelterCountAmount);
}
public void Addshelter()
{
totalCostShelter = 1500;
shelterCost = GetComponent<Text>();
shelterCost.text = totalCostShelter.ToString();
Debug.Log(totalCostShelter);
}
public void SubtractCountShelter()
{
if (shelterCountAmount >= 1)
{
shelterCountAmount -= 1;
shelterCount = GetComponent<Text>();
shelterCount.text = shelterCountAmount.ToString();
Debug.Log(shelterCountAmount);
}
else
{
shelterCountAmount = 0;
shelterCount = GetComponent<Text>();
shelterCount.text = shelterCountAmount.ToString();
}
}
public void Subtractshelter()
{
if (totalCostShelter >= 1500)
{
totalCostShelter -= 1500;
shelterCost = GetComponent<Text>();
shelterCost.text = totalCostShelter.ToString();
Debug.Log(totalCostShelter);
}
else
{
totalCostShelter = 0;
shelterCost = GetComponent<Text>();
shelterCost.text = totalCostShelter.ToString();
}
}
public void PurchaceShelter()
{
Debug.Log("before logic total cost is " totalCostShelter);
economy_worth -= totalCostShelter;
Debug.Log(economy_worth);
Debug.Log("total cost is " totalCostShelter);
Covid_PT -= shelterCountAmount * 80;
Debug.Log(Covid_PT);
Debug.Log("Num of vaccines: " shelterCountAmount);
}
//RESTRICTION
public void curfew()
{
Debug.Log("Economy Worth before curfew" economy_worth);
Debug.Log("Pateints before curfew" Covid_PT);
economy_worth -= 50;
Invoke("curfew", 1.0f);
Debug.Log("Economy Worth after curfew" economy_worth);
Debug.Log("Patients after curfew" Covid_PT);
}
public void strict()
{
Debug.Log("Economy Worth before strict lockdown" economy_worth);
Debug.Log("Pateints before strict lockdown" Covid_PT);
economy_worth -= 40;
Covid_PT ;
Invoke("strict", 1.0f);
Debug.Log("Economy Worth after strict lockdown" economy_worth);
Debug.Log("Patients after strict lockdown" Covid_PT);
}
public void mild()
{
Debug.Log("Economy Worth before mild lockdown" economy_worth);
Debug.Log("Pateints before mild lockdown" Covid_PT);
economy_worth -= 30;
Covid_PT = 2;
Invoke("mild", 1.0f);
Debug.Log("Economy Worth after mild lockdown" economy_worth);
Debug.Log("Patients after mild lockdown" Covid_PT);
}
public void smart()
{
Debug.Log("Economy Worth before smart lockdown" economy_worth);
Debug.Log("Pateints before smart lockdown" Covid_PT);
economy_worth -= 15;
Covid_PT = 3;
Invoke("smart", 1.0f);
Debug.Log("Economy Worth after smart lockdown" economy_worth);
Debug.Log("Patients after smart lockdown" Covid_PT);
}
public void NoLockdown()
{
Debug.Log("Economy Worth before No lockdown" economy_worth);
Debug.Log("Pateints before No lockdown" Covid_PT);
economy_worth -= 8;
Covid_PT = 5;
Invoke("NoLockdown", 1.0f);
Debug.Log("Economy Worth after No lockdown" economy_worth);
Debug.Log("Patients after No lockdown" Covid_PT);
}
//TAX COLLECTION
public void CollectTax()
{
if (Tax_amount == (int)(0.8 * population))
{
Debug.Log("Economy before collection: " economy_worth);
Debug.Log("Tax before collection: " Tax_amount);
economy_worth = Tax_amount;
// economyWorth.text = economy_worth.ToString();
// economyWorth = GetComponent<Text>();
Tax_amount = 0;
Debug.Log("Economy after collection: " economy_worth);
Debug.Log("Tax after collection: " Tax_amount);
}
else
{
Debug.Log("Tax not collected");
}
}
// S T O R E
public void item_1()
{
Debug.Log("Economy before Purchase" economy_worth);
economy_worth = 60000;
Debug.Log("Economy after Purchase" economy_worth);
}
public void item_2()
{
Debug.Log("Economy before Purchase" economy_worth);
economy_worth = 30000;
Debug.Log("Economy after Purchase" economy_worth);
}
public void item_3()
{
Debug.Log("Economy before Purchase" economy_worth);
economy_worth = 20000;
Debug.Log("Economy after Purchase" economy_worth);
}
public void item_4()
{
Debug.Log("Economy before Purchase" economy_worth);
economy_worth = 10000;
Debug.Log("Economy after Purchase" economy_worth);
}
void Update()
{ //MARKET
// Covid_Budget = (int)(0.01 * economy_worth);
// CovidBudget.text = Covid_Budget.ToString();
// CovidBudget = GetComponent<Text>();
//Debug.Log(totalCost12.ToString());
}
//CLAIM REWARD
public void Claim_1st()
{
if (claim1st!=0)
{
Debug.Log("Economy before reward" economy_worth);
economy_worth = claim1st;
Debug.Log("Economy after reward" economy_worth);
claim1st = 0;
}
else
{
Debug.Log("Award can't be claimed as it is: " claim1st);
}
}
public void Claim_2nd()
{
if (claim2nd != 0)
{
Debug.Log("Economy before reward" economy_worth);
economy_worth = claim2nd;
Debug.Log("Economy after reward" economy_worth);
claim2nd = 0;
}
else
{
Debug.Log("Award can't be claimed as it is: " claim2nd);
}
}
public void Claim_3rd()
{
if (claim3rd != 0)
{
Debug.Log("Economy before reward" economy_worth);
economy_worth = claim3rd;
Debug.Log("Economy after reward" economy_worth);
claim3rd = 0;
}
else
{
Debug.Log("Award can't be claimed as it is: " claim3rd);
}
}
public void Claim_4th()
{
if (claim4th != 0)
{
Debug.Log("Economy before reward" economy_worth);
economy_worth = claim4th;
Debug.Log("Economy after reward" economy_worth);
claim4th = 0;
}
else
{
Debug.Log("Award can't be claimed as it is: " claim4th);
}
}
public void Claim_5th()
{
if (claim5th != 0)
{
Debug.Log("Economy before reward" economy_worth);
economy_worth = claim5th;
Debug.Log("Economy after reward" economy_worth);
claim5th = 0;
}
else
{
Debug.Log("Award can't be claimed as it is: " claim5th);
}
}
public void Claim_6th()
{
if (claim6th != 0)
{
Debug.Log("Economy before reward" economy_worth);
economy_worth = claim6th;
Debug.Log("Economy after reward" economy_worth);
claim6th = 0;
}
else
{
Debug.Log("Award can't be claimed as it is: " claim6th);
}
}
}
public void Awake()
{ //NATIONAL BANK
economy_worth = 900000;
//Covid
Covid_PT = 9000;
//POPULATION
population = 160000;
//TAX COLLECTION
Tax_amount = (int)(0.8 * population);
}
}
Комментарии:
1. Из кода, который вы показали, все кажется в порядке. Показать полный код
2. хорошо, я добавил полный код в конце, пожалуйста, проверьте.
Ответ №1:
Можете ли вы сделать все переменные закрытыми, а затем посмотреть, обращается ли какой-либо другой код к тем переменным, о которых вы забыли. Я только что воспроизвел тот же код в консольном приложении, и мне кажется, что это нормально. Сделайте все переменные общедоступными и посмотрите, кто к ним обращается, потому что это должна быть единственная проблема, которую я могу увидеть.