In my Kingdom two crowns clone I have the following script, the problem is that when building a hammer, the first one still works normally, but from the second one onwards the hammers just spawn without the money being deducted and without the animation being executed
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Build : MonoBehaviour
{
[Header(“Coin Configuration”)]
public Transform SpawnPoint;
public GameObject CoinPrefab;
public int Cost;
private Queue<Transform> coinQueue = new Queue<Transform>();
private List<GameObject> spawnedCoins = new List<GameObject>();
private List<Transform> usedCoinPoints = new List<Transform>();
[Header("Automatic Point Detection")]
public List<Transform> CoinPointsParents = new List<Transform>();
public List<GameObject> Triggers = new List<GameObject>();
public List<Transform> BuildPointsParents = new List<Transform>();
private Transform currentCoinPointsParent;
private Transform currentBuildPointsParent;
[Header("Dependencies")]
public WoddWall WoodWall;
public Tree Tree;
public KingMovement King;
public WorkerManager WorkerManager;
public Base basebuild;
public GameObject Hammer;
private bool isBuilding = false;
private bool isInValidTrigger = false;
private int hammerIndex = 0;
void Start()
{
if (CoinPointsParents.Count > 0 && BuildPointsParents.Count > 0)
{
currentCoinPointsParent = CoinPointsParents[0];
currentBuildPointsParent = BuildPointsParents[0];
UpdateCoinQueue();
}
else
{
Debug.LogError("Keine Münz-Zielpunkte oder Baupunkte definiert! Bitte die Listen im Inspector setzen.");
}
if (WorkerManager == null)
{
WorkerManager = FindObjectOfType<WorkerManager>();
}
}
void Update()
{
if (Input.GetKeyDown(KeyCode.E))
{
TryBuild();
}
}
public void TryBuild()
{
if (!isInValidTrigger || isBuilding || currentBuildPointsParent == null)
{
Debug.Log("Bauen ist nur in einem gültigen Trigger-Bereich möglich oder der Bau läuft bereits.");
return;
}
if (!AreBuildPointsActive())
{
Debug.Log("Keine aktiven Baupunkte vorhanden. Münzen können nicht ausgegeben werden.");
return;
}
Cost = currentCoinPointsParent.childCount;
if (King.coins >= Cost)
{
StartCoroutine(PayAndBuild());
}
else
{
Debug.Log("Nicht genügend Münzen, um zu bauen.");
}
}
private IEnumerator PayAndBuild()
{
if (currentBuildPointsParent.CompareTag("WorkerBuy") && hammerIndex >= currentBuildPointsParent.childCount)
{
ToggleCoinPointsVisibility(false);
Debug.Log("Keine freien Bauplätze mehr für den Hammer. Münzen können nicht mehr ausgegeben werden.");
yield break;
}
isBuilding = true;
Cost = currentCoinPointsParent.childCount;
Debug.Log($"Baukosten: {Cost}, Verfügbare Münzen: {King.coins}");
while (coinQueue.Count > 0 && Cost > 0 && King.coins > 0)
{
// Reduziere Münzen
King.coins--;
King.RemoveCoinFromBag();
Debug.Log($"Münze entfernt. Verbleibende Münzen: {King.coins}");
Transform targetPoint = coinQueue.Dequeue();
if (usedCoinPoints.Contains(targetPoint))
{
Debug.LogWarning($"Punkt {targetPoint.name} wurde bereits verwendet, überspringe.");
continue;
}
usedCoinPoints.Add(targetPoint);
GameObject coin = Instantiate(CoinPrefab, SpawnPoint.position, Quaternion.identity);
spawnedCoins.Add(coin);
yield return StartCoroutine(MoveCoinToPoint(coin, targetPoint));
Cost--;
}
StartBuild();
if (!currentBuildPointsParent.CompareTag("WorkerBuy"))
{
Destroy(currentCoinPointsParent.gameObject);
}
isBuilding = false;
}
private IEnumerator MoveCoinToPoint(GameObject coin, Transform targetPoint)
{
Debug.Log($"Münze bewegt sich zu Ziel: {targetPoint.position}");
float duration = 0.5f;
Vector3 startPosition = coin.transform.position;
for (float elapsed = 0; elapsed < duration; elapsed += Time.deltaTime)
{
coin.transform.position = Vector3.Lerp(startPosition, targetPoint.position, elapsed / duration);
yield return null;
}
coin.transform.position = targetPoint.position;
Debug.Log($"Münze angekommen bei {coin.transform.position}");
}
private void StartBuild()
{
foreach (GameObject coin in spawnedCoins)
{
Destroy(coin);
}
spawnedCoins.Clear();
Debug.Log("Bau gestartet!");
if (currentBuildPointsParent.CompareTag("WorkerBuy"))
{
StartCoroutine(PlaceHammer());
}
else
{
foreach (Transform point in usedCoinPoints)
{
point.gameObject.SetActive(false);
}
usedCoinPoints.Clear();
HandleOtherBuilds();
if (!AreBuildPointsActive())
{
Debug.Log("Alle Baupunkte ausgeblendet. Bauen wird deaktiviert.");
isBuilding = false;
return;
}
CheckAndToggleBuildPointsVisibility();
}
}
IEnumerator PlaceHammer()
{
if (hammerIndex < currentBuildPointsParent.childCount)
{
Transform buildPoint = currentBuildPointsParent.GetChild(hammerIndex);
// Überprüfen, ob Münzen verfügbar sind
if (King.coins <= 0)
{
Debug.Log("Nicht genügend Münzen verfügbar, um einen weiteren Hammer zu platzieren.");
yield break;
}
// Münze abziehen
King.coins--;
King.RemoveCoinFromBag();
// Hammer instanziieren
Instantiate(Hammer, buildPoint.position, Quaternion.Euler(0, 0, 100));
hammerIndex++;
Debug.Log($"Hammer an Punkt {buildPoint.position} platziert. (Index: {hammerIndex})");
if (hammerIndex >= currentBuildPointsParent.childCount)
{
Debug.Log("Keine weiteren Bauplätze für den Hammer vorhanden.");
}
}
}
private void HandleOtherBuilds()
{
if (basebuild != null && currentBuildPointsParent.CompareTag("Base"))
{
foreach (Transform buildPoint in currentBuildPointsParent)
{
basebuild.BaseBuild(buildPoint, currentBuildPointsParent.GetChild(1).gameObject);
break;
}
}
if (Tree != null && currentBuildPointsParent.CompareTag("Tree"))
{
foreach (Transform buildPoint in currentBuildPointsParent)
{
Destroy(currentBuildPointsParent.GetChild(1).gameObject);
Tree.BuildTree(buildPoint);
break;
}
}
if (WorkerManager != null)
{
foreach (Transform buildPoint in currentBuildPointsParent)
{
WorkerManager.AddBuildOrder(buildPoint, currentBuildPointsParent.tag);
break;
}
}
}
private void OnTriggerEnter2D(Collider2D other)
{
int triggerIndex = Triggers.IndexOf(other.gameObject);
if (triggerIndex >= 0 && triggerIndex < CoinPointsParents.Count && triggerIndex < BuildPointsParents.Count)
{
isInValidTrigger = true;
currentCoinPointsParent = CoinPointsParents[triggerIndex];
currentBuildPointsParent = BuildPointsParents[triggerIndex];
UpdateCoinQueue();
ToggleCoinPointsVisibility(true);
if (currentBuildPointsParent.CompareTag("WorkerBuy"))
{
hammerIndex = 0;
}
}
}
private void OnTriggerExit2D(Collider2D other)
{
if (Triggers.Contains(other.gameObject))
{
isInValidTrigger = false;
ToggleCoinPointsVisibility(false);
}
}
private void ToggleCoinPointsVisibility(bool isVisible)
{
if (currentCoinPointsParent != null)
{
foreach (Transform point in currentCoinPointsParent)
{
Renderer renderer = point.GetComponent<Renderer>();
if (renderer != null)
{
renderer.enabled = isVisible;
}
}
}
}
private void UpdateCoinQueue()
{
coinQueue.Clear();
if (currentCoinPointsParent != null)
{
foreach (Transform point in currentCoinPointsParent)
{
if (!usedCoinPoints.Contains(point)) // Nur unbenutzte Punkte hinzufügen
{
coinQueue.Enqueue(point);
}
}
}
Debug.Log($"Aktualisierte Münz-Queue: {coinQueue.Count} Punkte verfügbar.");
}
private void CheckAndToggleBuildPointsVisibility()
{
bool arePointsAvailable = false;
foreach (Transform buildPoint in currentBuildPointsParent)
{
if (buildPoint.gameObject.activeSelf)
{
arePointsAvailable = true;
break;
}
}
foreach (Transform buildPoint in currentBuildPointsParent)
{
buildPoint.gameObject.SetActive(arePointsAvailable);
}
}
private bool AreBuildPointsActive()
{
if (currentBuildPointsParent != null)
{
foreach (Transform buildPoint in currentBuildPointsParent)
{
if (buildPoint.gameObject.activeSelf)
{
return true;
}
}
}
return false;
}
}