i have an EmptyObject called "Training Area", in this object i have Player, Floor,…
- User
i have an EmptyObject called "Training Area", in this object i have Player, Floor, WallTop, WallBottom, WallRight, WallLeft and all spawning foods. when i have more than 1 "Training Area", all food from other Training Areas spawns in first Training Area
here's FoodSpawner.cs, which in "Training Area": using System.Collections; using UnityEngine; using Unity.MLAgents;
public class FoodSpawner : MonoBehaviour { public GameObject foodPrefab; public int numberOfFoods; public float spawnRange; public float maxEpisodeTime = 30f; // in seconds private float episodeTimer;
private PlayerController player; private void Start() { player = GetComponentInChildren<PlayerController>(); if (player != null) { player.SetFoodSpawner(this); } SpawnFood(); episodeTimer = maxEpisodeTime; } void Update() { ManageEpisode(); } public void SpawnFood(int customNumberOfFoods = -1) { // Delete any remaining food foreach (var food in GetComponentsInChildren<Food>()) { Destroy(food.gameObject); } int foodCount = customNumberOfFoods != -1 ? customNumberOfFoods : numberOfFoods; for (int i = 0; i < foodCount; i++) { Vector2 spawnPosition = new Vector2(Random.Range(-spawnRange, spawnRange), Random.Range(-spawnRange, spawnRange)); GameObject newFood = Instantiate(foodPrefab, spawnPosition, Quaternion.identity, transform); newFood.transform.localScale = Vector3.one; } } void ManageEpisode() { episodeTimer -= Time.deltaTime; int remainingFoodCount = GetComponentsInChildren<Food>().Length; if (episodeTimer <= 0 || remainingFoodCount == 0) { player.ResetPlayerEpisode(); SpawnFood(); episodeTimer = maxEpisodeTime; } }}
- Assistant
It seems the issue is related to the fact that all food objects are instantiated as children of the first "Training Area" object found in the scene. To fix that, follow these steps:
- Make sure that each "Training Area" object has its own FoodSpawner script attached;
- Change the "foodPrefab" field in the script to be a child of the current "Training Area" object (i.e., its parent object);
- When spawning food, only spawn it in the current "Training Area".
To implement these changes, follow these steps:
- Make sure each "Training Area" has its own FoodSpawner script attached;
- Set the "foodPrefab" field in the FoodSpawner script to be a child of the "Training Area" object;
- Modify the FoodSpawner script's SpawnFood method as shown below:
public void SpawnFood(int customNumberOfFoods = -1) { // Delete any remaining food foreach (var food in GetComponentsInChildren<Food>()) { Destroy(food.gameObject); } int foodCount = customNumberOfFoods != -1 ? customNumberOfFoods : numberOfFoods; for (int i = 0; i < foodCount; i++) { Vector2 spawnPosition = new Vector2(Random.Range(-spawnRange, spawnRange), Random.Range(-spawnRange, spawnRange)); GameObject newFood = Instantiate(foodPrefab, spawnPosition, Quaternion.identity); newFood.transform.SetParent(transform); // Set the parent to the current "Training Area" newFood.transform.localScale = Vector3.one; } }
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.