- Home /
Value of Enum changes randomly
This is my Code, I've talked with many people and noone knows what's going on I'm looping through an array of robot parts and when I debug.Log inside the loop everything is fine, there is one left arm and one right arm. But when I loop through it outside of the for-loop it shows two right arms, with literally no code inbetween. It always changes from Leftarm to rightarm, never any other part.
private RobotPartData[] TrySelectPartsFromWallet(RobotDefinitionData robotDefinitionData)
{
List<RobotPartData> robotPartDatas = new List<RobotPartData>();
foreach (var equippableData in robotDefinitionData.equippableDatas)
{
RobotPartData robotPartData = WalletCatalogue.Instance.GetRobotPart(equippableData.nftID);
robotPartData.SetEquipSlot(equippableData.equipSlot);
robotPartDatas.Add(robotPartData);
Debug.LogError($"{robotPartData.equipSlot}");
}
foreach (var robotPartData in robotPartDatas)
{
Debug.LogError($"{robotPartData.equipSlot}");
}
return robotPartDatas.ToArray();
}
I'm not even sure if this is unity specific or a c# compiler bug. The equipslot variable is just an Enum, and isn't getting accessed inbetween, everything is running synchronously
Answer by unity_ek98vnTRplGj8Q · Jun 16, 2021 at 10:40 PM
It looks like when you call WalletCatalogue.Instance.GetRobotPart(equippableData.nftID);
it is returning the same object for both the Left Arm Equippable Data and the Right Arm Equippable Data. When you loop through it the first time it grabs the left arm object and tells it that it is a left arm object, then logs it. Then when you ask for the Right Arm robot part with the GetRobotPart() it gives you the same object that you just said was a Left Arm robot part, so it overwrites equipSlot with Right Arm then adds it to the list again. Now you have the same object in the list twice in a row, and that object has most recently been given an equip slot value of Right Arm.
To fix you need to make sure that your call to GetRobotPart is actually returning 2 distinct objects when you give it the left and right arm nftID values.