- Home /
Prefab Variables Problem
Hello! I have unfortunately ran into another problem with my building system. So, i made a system so that in order to place a building, you must design the interior first in a form of a blueprint. When i place a piece of machinery or a furniture, i want to add the resources required to make those in a list of resources that will be required to construct the building. For example, a blueprint for a factory with 3 machines that use 20 iron each will need 60 iron to be built. Every time i place a piece of furniture, i make it run through the list of resources required to construct the building to see if the resources for the furniture already exist in the list (for example, if in the list of required resources for the blueprint there are 20 iron and 40 copper, and i want to place a machine down, which costs 30 iron to make, it will check the list of required resources to see if i already have iron in it, and if i do, it will add the total cost. So now, it will cost 50 iron and 40 copper to build.) So, lets get to the problem, if you have figured out what i mean by now. The problem is that, i have every piece of furniture as a prefab, and each one has a Building script in it, that says how many resources it requires. Every time my machine runs the list check in the resources list for the blueprint, it adds the amount (Furniture : 30 iron, list : 20 iron, so it adds 30 + 20 = 50 iron) in BOTH the list and the prefab. So, the furniture that needed 30 iron to be placed now needs 60!. Can someone please help me to bypass this problem? any kind of help is appreciated. The code (didnt forget this time haha :) ): The script in the prefab: using System.Collections; using System.Collections.Generic; using UnityEngine;
public enum BuildingType {Building, Interior }
public enum BuildingSize {Small, Medium, Large}
public class Building : MonoBehaviour
{
public BuildingType type;
public BuildingSize size;
public GameObject building;
public int cost;
public List<ResourceItem> required = new List<ResourceItem>();
}
The script part responsible for adding the required resources:
void AddRequiredResources()
{
if (thisBlueprint.requiredResources.Capacity != 0)
{
foreach (ResourceItem resource in thisBlueprint.requiredResources)
{
foreach (ResourceItem required in thisBuilding.required)
{
if (thisBlueprint.requiredResources.Contains(required))
{
resource.amount += required.amount;
}
else
{
thisBlueprint.requiredResources.Add(resource);
}
}
}
}
else
{
thisBlueprint.requiredResources.AddRange(thisBuilding.required);
}
}
Its only a personal commment:
So long post, its hard to read the text and the code, I will not do it.
Try to optimize what you say and what you ask, if want to get answers...
Bye!
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Tower defense target update 0 Answers
Can you call a coroutine from a script on a prefab? 1 Answer