Foreach итерирует объект по нескольку раз
Столкнулся с проблемой во время вставления значений в поля. При работе скриптов ниже, когда в коде для персонажа я вызываю дебаг лог, чтобы вывести сообщение в unity, я получаю "0 and 0", что значит, что все поля сбросились по какой то причине. Я проверил отладкой, и да, они действительно сбрасываются после какого то события. К тому же оказалось, что итерируются эти две "foreach" по нескольку раз, и каждую итерацию значения сбрасываются.
Мои вопросы такие: 1.Почему они сбрасываются? 2. Как сделать итерацию разовой, а не несколько раз? 3.Действительно ли при каждой итерации поля в "foreach" сбрасываются, или что то другое?
XML файл для монстра:
<?xml version="1.0" encoding="utf-8"?>
<Stats>
<EStats>
<Name>"Monster"</Name>
<Health>10</Health>
<Strength>2</Strength>
</EStats>
</Stats>
Код с монстром:
using System.Collections;
using System.Collections.Generic;
using System.Xml.Linq;
using UnityEngine;
namespace DarkestBone
{
public class Monster_Stats : MonoBehaviour
{
public string Monster_Name;
public int Monster_Health;
public int Monster_Strength;
void Start()
{
XDocument Xdoc = XDocument.Load("D:\\unity projects\\DarkestBone\\Assets\\Monster_Stats.xml");
XElement Stats = Xdoc.Element("Stats");
foreach (XElement EStats in Stats.Elements("EStats"))
{
XElement Name = EStats.Element("Name");
XElement HP = EStats.Element("Health");
XElement STR = EStats.Element("Strength");
string Monster_Name = Name.Value;
int.TryParse(HP.Value, out Monster_Health);
int.TryParse(STR.Value, out Monster_Strength);
}
}
void Update()
{
}
}
}
XML файл для персонажа:
<?xml version="1.0" encoding="utf-8"?>
<Stats>
<EStats>
<Name>"Mendzek"</Name>
<Health>10</Health>
<Willpower>5</Willpower>
<Body>5</Body>
<Strength>3</Strength>
<Speed>2</Speed>
<Skill>1</Skill>
<Intuition>1</Intuition>
<Faith>8</Faith>
</EStats>
</Stats>
Код с персонажем:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
using System.IO;
using System.Xml.Linq;
namespace DarkestBone {
public class MC_Stats : MonoBehaviour
{
public GameObject MC;
Monster_Stats Monster_Stats = new Monster_Stats();
public string MC_Name;
public int MC_Health;
public int MC_Willpower;
public int MC_Body;
public int MC_Strength;
public int MC_Speed;
public int MC_Skill;
public int MC_Intuition;
public int MC_Faith;
public void Start()
{
XDocument Xdoc = XDocument.Load("D:\\unity projects\\DarkestBone\\Assets\\MC_Stats.xml");
XElement Stats = Xdoc.Element("Stats");
foreach (XElement EStats in Stats.Elements("EStats"))
{
XElement Name = EStats.Element("Name");
XElement HP = EStats.Element("Health");
XElement WIL = EStats.Element("Willpower");
XElement BDY = EStats.Element("Body");
XElement STR = EStats.Element("Strength");
XElement SPD = EStats.Element("Speed");
XElement SKL = EStats.Element("Skill");
XElement INT = EStats.Element("Intuition");
XElement FTH = EStats.Element("Faith");
MC_Name = Name.Value;
int.TryParse(HP.Value, out MC_Health);
int.TryParse(STR.Value, out MC_Strength);
int.TryParse(WIL.Value, out MC_Willpower);
int.TryParse(BDY.Value, out MC_Body);
int.TryParse(SPD.Value, out MC_Speed);
int.TryParse(SKL.Value, out MC_Skill);
int.TryParse(INT.Value, out MC_Intuition);
int.TryParse(FTH.Value, out MC_Faith);
}
}
public void Attack()
{
Monster_Stats.Monster_Health = Monster_Stats.Monster_Health - MC_Strength;
Debug.Log($" {MC_Name} attacking {Monster_Stats.name} and deal {MC_Strength} damage. {Monster_Stats.Monster_Name} have {Monster_Stats.Monster_Health} HP now.");
Debug.Log($"{MC_Strength} and {Monster_Stats.Monster_Strength}"); //выводит "0 and 0", хотя не должно
}
public void Update()
{
}
}
}