Hello,
I have a problem. I am trying to make a building system, and i want to make it so you can only build something, if you have the resources for it. E.g you can build a house only if you have 10 copper and 10 iron. I have made a list with all the resources the player can obtain, and a list for all the resources the building requires. I have been trying for the past 3 days to make it so you can build the house when you have BOTH 10 copper and iron. Can you help me? Any kind of help is appreciated.
Code(So dumb for not doing it in the first place) :
The building script :
using UnityEngine;
public class BuildingScript : MonoBehaviour
{
public GameObject item;
Building thisBuilding;
ResourcesSystem inv;
public bool canBuild;
private void Start()
{
inv = GameObject.FindObjectOfType<ResourcesSystem>();
}
private void Update()
{
Build();
}
void Build()
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (Input.GetMouseButton(0))
{
UpdateComponent();
if (Physics.Raycast(ray, out hit))
{
if (hit.collider.tag == "Ground")
{
for (int i = 0; i < inv.resources.Length; i++)
{
if(inv.resources*.resource == thisBuilding.required.resource)*
-
{*
_ if(inv.resources*.amount >= thisBuilding.required.amount)_
_ {_
_ Vector3 itemPos = item.transform.position;*_
* itemPos.x = Mathf.Round(hit.point.x);*
* itemPos.y = Mathf.Round(hit.point.y) + (item.transform.localScale.y / 2 + .5f);*
* itemPos.z = Mathf.Round(hit.point.z);*
* item.transform.position = itemPos;*
* Instantiate(item);*
_ inv.resources*.amount -= thisBuilding.required.amount;
}
}
else*
* {
continue;
}
}
}
}
}
}*_
* void UpdateComponent()*
* {*
* thisBuilding = item.GetComponent();*
* }*
}
I made it so it only required one type of resource to build.
Resources System :
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ResourcesSystem : MonoBehaviour
{
* public ResourceItem[] resources;*
}
[System.Serializable]
public class ResourceItem
{
* public Resource resource;*
* public int amount;*
}
The single resource (I made it as a scriptable object, because i want to add more resources in the future)
:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[CreateAssetMenu(menuName = “Resource”, fileName = “New Resource” , order = 1)]
public class Resource : ScriptableObject
{
* public string Name;*
}
The script in each building:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Building : MonoBehaviour
{
* public GameObject building;*
* public int cost;*
* public ResourceItem required;*
}
Thats all there is. If you cant figure out how to make it so it requires multiple resources to build, i will just rewrite it differently.