I have copied my TrainingArea many times, and while training, it seems to me that when…
- User
I have copied my TrainingArea many times, and while training, it seems to me that when other agents eat their food, it disappears from the first agent. I may be wrong and it may not be. But I know for sure that the food disappears by itself from the very first agent.
here's my TrainingArea.cs: using UnityEngine; using System.Collections.Generic;
public class TrainingArea : MonoBehaviour { [SerializeField] private GameObject playerPrefab; [SerializeField] private GameObject foodPrefab; [SerializeField] private int numberOfFoods = 10; [SerializeField] private float spawnRadius = 5.0f; private PlayerAgent playerAgent; private List foodInstances = new List();
private void Start() { SpawnPlayer(); SpawnFoods(); } public void ResetArea() { ResetPlayer(); ResetFoods(); } private void ResetPlayer() { if (playerAgent != null) { playerAgent.transform.localPosition = new Vector3(Random.Range(-spawnRadius, spawnRadius), Random.Range(-spawnRadius, spawnRadius), 0); } } public void ResetFoods() { List<GameObject> eatenFoods = new List<GameObject>(); foreach (GameObject food in foodInstances) { if (food != null) { food.transform.localPosition = GetValidSpawnPosition(); } else { eatenFoods.Add(food); } } foreach (GameObject food in eatenFoods) { Vector3 spawnPosition = GetValidSpawnPosition(); GameObject foodInstance = Instantiate(foodPrefab, spawnPosition, Quaternion.identity, transform); foodInstances.Add(foodInstance); } // Remove eaten food references from the list foodInstances.RemoveAll(food => food == null); } private void SpawnPlayer() { Vector3 randomPosition = GetValidSpawnPosition(); GameObject playerInstance = Instantiate(playerPrefab, randomPosition + transform.position, Quaternion.identity, transform); playerAgent = playerInstance.GetComponent<PlayerAgent>(); } private void SpawnFoods() { for (int i = 0; i < numberOfFoods; i++) { Vector3 spawnPosition = GetValidSpawnPosition(); GameObject foodInstance = Instantiate(foodPrefab, spawnPosition, Quaternion.identity, transform); foodInstances.Add(foodInstance); } } private Vector3 GetValidSpawnPosition() { Vector3 spawnPosition; do { spawnPosition = new Vector3(Random.Range(-spawnRadius, spawnRadius), Random.Range(-spawnRadius, spawnRadius), 0); } while (playerAgent != null && Vector3.Distance(playerAgent.transform.localPosition, spawnPosition) <= 5.0f); return spawnPosition; } public bool AllFoodEaten() { foreach (GameObject food in foodInstances) { if (food != null) { return false; } } return true; }}
here's my PlayerAgent.cs: using UnityEngine; using Unity.MLAgents; using Unity.MLAgents.Actuators; using Unity.MLAgents.Sensors;
public class PlayerAgent : Agent { [SerializeField] private float moveSpeed = 5.0f; [SerializeField] private float maxEpisodeTime = 20.0f; private TrainingArea trainingArea; private float episodeTime = 0.0f;
- Assistant
It seems that your issue might be related to how you handle resetting the foods in the ResetFoods method. The issue occurs because you are adding the eaten foods to a new list and then spawning new food instances again.
To fix this issue, you can change the ResetFoods method so that it re-spawns the eaten food correctly, as shown below:
public void ResetFoods() { List<GameObject> eatenFoods = new List<GameObject>(); foreach (GameObject food in foodInstances) { if (food != null) { // If the food is still in the game, update its position food.transform.localPosition = GetValidSpawnPosition(); } else { // If the food has been eaten, add it to the list of eatenFoods eatenFoods.Add(food); } } // Loop through the list of eatenFoods for (int i = 0; i < eatenFoods.Count; i++) { // Calculate a valid spawn position Vector3 spawnPosition = GetValidSpawnPosition(); // Instantiate a new food object at the spawn position and set its parent to the TrainingArea transform. GameObject foodInstance = Instantiate(foodPrefab, spawnPosition, Quaternion.identity, transform); // Replace the eaten food instance with the new food instance in the foodInstances list int eatenFoodIndex = foodInstances.IndexOf(eatenFoods[i]); foodInstances[eatenFoodIndex] = foodInstance; } // You no longer need this line since eaten foods have been correctly replaced //foodInstances.RemoveAll(food => food == null); }
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.