# Building system problem

Hello!
I have already posted about this problem once, but i am rewriting about it because it is still there, despite my best efforts.
So, a few days ago, i mentioned about me making a building system in my game called Mars. The system is really simple:
-In order to be able to place a building, you need a blueprint for it.
-When you create a new blank blueprint, you must add interior items to make it funtional. These items require resources to make.
-Once you are done with making the blueprint, you click “Apply”.
Once you click Apply, the system calculates how many resources it costs in total, and then adds them to the required resources for the building.
For example , say you want to build a house. You will need a blueprint for it. When you create a blueprint, you need to add everything an average house has(a bed, a stove, a toilet ect). Lets say you add a stove and a bed. The stove costs 20 iron, and the bed costs 10 iron, 20 wood and 20 cotton. When you are done, the system calculates the required resources and adds them to the blueprint. So, now in order to build that house, you need 30 iron, 20 wood and 20 cotton.
Now, lets get to the problem. Every time i place an interior item in the blueprint, and the resources for it already exist in the list(like, placing a stove, which costs 20 iron, and the list already contains 10 iron), i made it so it stacks(so now, the bleprint requires 30 iron) . But, it adds it to BOTH the resource list and the required resources for the building.
If you didnt get it, thats fine, here’s an example of the problem. Lets take the house from before. You create the blueprint and you place a stove. So, currently the stove and the blueprint both cost 20 iron. You place another stove. Now the stove costs 40 iron! You place a third one and now it costs 80!.
The stove is a prefab, so i dont know if that matters.
Can someone help me?
The code:
For building(i will filter out the unimportant ones)

``````    //Code improved by Casiell

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Linq;

public class BuildingInteriorScript : MonoBehaviour
{
public GameObject item;
Building thisBuilding;
ResourcesSystem inv;
public Blueprint thisBlueprint;
public bool canBuild;

private void Update()
{
BuildInterior();
}

void BuildInterior()
{
(Building code filtered out)
Instantiate(item);
CalculateResources();// The problem
}
#region Errors(filtered out)

#endregion
}
}

{

}

void UpdateComponent()(filtered out)
{

}

public void CalculateResources()// Here is the problem
{
if (thisBlueprint.requiredResources.Capacity != 0)
{
foreach (ResourceItem resource in thisBuilding.required)
{
foreach (ResourceItem required in thisBlueprint.requiredResources)
{

if (thisBlueprint.requiredResources.Contains(resource))
{
required.amount += resource.amount;
}
else
{
}
}
}
}
else
{
}
}
}
``````

what happens when you call requiredResources.Add() ? does it create a copy of what you’re adding or does it keep the initial object ? my guess is the later, and then when you += the amount again it gets added to itself.

also the double foreach loop are probably missing a test somewhere to check the type of resource that is being added,
if building costs:
Iron 10
Wood 20
gold 30

second time you add that cost to you blueprint its going to call:

``````for (iron in building) for (iron in blueprint)  add iron
for (Wood in blueprint)  add Wood
for (iron in blueprint)  add gold

for (Wood in building) for (iron in blueprint)  add iron
for (Wood in blueprint)  add Wood
for (iron in blueprint)  add gold

for (gold in building) for (iron in blueprint)  add iron
for (Wood in blueprint)  add Wood
for (iron in blueprint)  add gold
``````