using System.Collections; using System.Collections.Generic; using UnityEngine; using…
- User
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.SceneManagement;
public class Chest : MonoBehaviour { public float delayTime = 5f; public GameObject bowPrefab; public GameObject pistolPrefab; public GameObject healthPickupPrefab;
private void OnTriggerEnter2D(Collider2D collision) { if (collision.CompareTag("Player")) { // Generate a random number (between 0 and 1) to determine if the weapon spawns or not float randomValue = Random.value; if (SceneManager.GetActiveScene().buildIndex == 0) { if (randomValue <= 0.5f) { // Spawn a bow GameObject bow = Instantiate(bowPrefab, collision.transform); bow.transform.localPosition = new Vector3(-0.25f, -0.25f, 0f); bow.transform.localRotation = Quaternion.identity; } } else if (SceneManager.GetActiveScene().buildIndex == 1) { if (randomValue <= 0.5f) { // Spawn a pistol GameObject pistol = Instantiate(pistolPrefab, collision.transform); pistol.transform.localPosition = new Vector3(-0.47f, -0.09f, 0f); pistol.transform.localRotation = Quaternion.identity; } } // Generate a random number (between 0 and 1) to determine if a health pickup spawns or not float randomValue2 = Random.value; if (randomValue2 <= 0.3f) { // Spawn a HealthPickup GameObject healthPickup = Instantiate(healthPickupPrefab); healthPickup.transform.localPosition = transform.position; healthPickup.transform.localRotation = Quaternion.identity; } // Disable the chest to prevent spawning multiple weapons gameObject.SetActive(false); // Enable the chest after a delay Invoke("EnableChest", delayTime); } } private void EnableChest() { gameObject.SetActive(true); }} make it so instead of spawning the bow, set to active
- Assistant
To set the bow active instead of spawning it, you can modify the code as follows:
// Remove the bowPrefab variable declaration private void OnTriggerEnter2D(Collider2D collision) { if (collision.CompareTag("Player")) { // Generate a random number (between 0 and 1) to determine if the weapon spawns or not float randomValue = Random.value; if (SceneManager.GetActiveScene().buildIndex == 0) { if (randomValue <= 0.5f) { // Set the bow object active collision.transform.Find("Bow").gameObject.SetActive(true); } } else if (SceneManager.GetActiveScene().buildIndex == 1) { if (randomValue <= 0.5f) { // Set the pistol object active collision.transform.Find("Pistol").gameObject.SetActive(true); } } // Generate a random number (between 0 and 1) to determine if a health pickup spawns or not float randomValue2 = Random.value; if (randomValue2 <= 0.3f) { // Spawn a HealthPickup GameObject healthPickup = Instantiate(healthPickupPrefab); healthPickup.transform.localPosition = transform.position; healthPickup.transform.localRotation = Quaternion.identity; } // Disable the chest to prevent spawning multiple weapons gameObject.SetActive(false); // Enable the chest after a delay Invoke("EnableChest", delayTime); } } - User
write out the entire script
- Assistant
Certainly! Here's the modified script with the entire content:
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.SceneManagement; public class Chest : MonoBehaviour { public float delayTime = 5f; public GameObject healthPickupPrefab; private void OnTriggerEnter2D(Collider2D collision) { if (collision.CompareTag("Player")) { // Generate a random number (between 0 and 1) to determine if the weapon spawns or not float randomValue = Random.value; if (SceneManager.GetActiveScene().buildIndex == 0) { if (randomValue <= 0.5f) { // Set the bow object active collision.transform.Find("Bow").gameObject.SetActive(true); } } else if (SceneManager.GetActiveScene().buildIndex == 1) { if (randomValue <= 0.5f) { // Set the pistol object active collision.transform.Find("Pistol").gameObject.SetActive(true); } } // Generate a random number (between 0 and 1) to determine if a health pickup spawns or not float randomValue2 = Random.value; if (randomValue2 <= 0.3f) { // Spawn a HealthPickup GameObject healthPickup = Instantiate(healthPickupPrefab); healthPickup.transform.localPosition = transform.position; healthPickup.transform.localRotation = Quaternion.identity; } // Disable the chest to prevent spawning multiple weapons gameObject.SetActive(false); // Enable the chest after a delay Invoke("EnableChest", delayTime); } } private void EnableChest() { gameObject.SetActive(true); } }