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();
codeprivate 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; } - Assistant
It seems that the issue is that you are instantiating food instances for each training area but they are all part of one
foodInstanceslist. If a food instance being removed is causing issues in another training area, it means their food instances should be managed separately.I would suggest creating a separate class like
TrainingFood, and keep the food instances for each training area in different objects of this class.Here is a possible structure for the
TrainingFoodclass and the associated changes inTrainingArea: - User
- Assistant
Good job! Now you have integrated the
TrainingFoodclass into yourTrainingAreaclass.I noticed a small issue in
ResetFoodsfunction. You are still using the oldfoodInstanceslist instead oftrainingFood.foodInstances. You should replace all the references of in the function to : - User
you broke the code, now all food spawns in the very first area, and you can still eat if a player from another zone moves to a similar place in his zone. I can't fully describe, but you broke everything.
- Assistant
Apologies for the confusion! It seems that I misunderstood the original issue.
Let's revert the TrainingArea back to your initial version without the TrainingFood changes I suggested earlier. Once you have reverted the code, I will try to address the problem differently.
In order to properly address the problem, I have some follow-up questions:
- Are you observing the other agent's food disappear when the first agent eats food?
- Are the Training Areas completely separated from each other (no overlap)?
- Are all instantiated objects (PlayerAgents, foodInstances, etc.) properly separated as children of their respective TrainingArea objects?
These questions would help me understand the exact problem better and provide an appropriate solution.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.