using UnityEngine;
public class BookManager : MonoBehaviour
{
public GameObject redBookPrefab;
public GameObject blueBookPrefab;
public GameObject greenBookPrefab;
public Transform[] spawnLocations;
public Collider redZone;
public Collider blueZone;
public Collider greenZone;
private GameObject redBook;
private GameObject blueBook;
private GameObject greenBook;
private HotelSpawner hotelSpawner;
void Start()
{
hotelSpawner = FindObjectOfType<HotelSpawner>();
SpawnBooks();
}
void SpawnBooks()
{
// Shuffle locations
ShuffleArray(spawnLocations);
// Spawn books at random locations
redBook = Instantiate(redBookPrefab, spawnLocations[0].position, spawnLocations[0].rotation);
blueBook = Instantiate(blueBookPrefab, spawnLocations[1].position, spawnLocations[1].rotation);
greenBook = Instantiate(greenBookPrefab, spawnLocations[2].position, spawnLocations[2].rotation);
Debug.Log("Books spawned");
// Ensure the books have Rigidbody components
AddRigidbodyIfNeeded(redBook);
AddRigidbodyIfNeeded(blueBook);
AddRigidbodyIfNeeded(greenBook);
}
void ShuffleArray(Transform[] array)
{
for (int i = 0; i < array.Length; i++)
{
Transform temp = array[i];
int randomIndex = Random.Range(i, array.Length);
array[i] = array[randomIndex];
array[randomIndex] = temp;
}
}
void AddRigidbodyIfNeeded(GameObject book)
{
if (book.GetComponent<Rigidbody>() == null)
{
book.AddComponent<Rigidbody>().isKinematic = true;
}
}
private void OnTriggerStay(Collider other)
{
Debug.Log("OnTriggerStay called with " + other.gameObject.name);
bool redBookPlaced = redZone.bounds.Contains(redBook.transform.position);
bool blueBookPlaced = blueZone.bounds.Contains(blueBook.transform.position);
bool greenBookPlaced = greenZone.bounds.Contains(greenBook.transform.position);
Debug.Log("RedBookPlaced: " + redBookPlaced);
Debug.Log("BlueBookPlaced: " + blueBookPlaced);
Debug.Log("GreenBookPlaced: " + greenBookPlaced);
CheckBooksPlaced(redBookPlaced, blueBookPlaced, greenBookPlaced);
}
void CheckBooksPlaced(bool redBookPlaced, bool blueBookPlaced, bool greenBookPlaced)
{
if (redBookPlaced && blueBookPlaced && greenBookPlaced)
{
hotelSpawner.IncrementWinCount();
Debug.Log("All books placed correctly!");
}
}
}
i feel like im missing something obvious, but no matter what i do, i cannot get the books to be recognised in the zones, its meant to be i bring the books and place them in the zone, when all three are there im done, but nothing i do seems to work