I have some code that is meant to dequeue troops and queue them. When I click on the menu items, they are meant to decrease and destroy if they are smaller than 1 and they do this fine but, my lists are acting up, they seem to be empty when in debug inspector mode, they have 1 entry but it’s missing and I tried to reset that but it doesn’t work. I don’t know what to do about this and I really want to fix this. The problem is in the dequeue function. Here is my code:
using UnityEngine;
using UnityEngine.Rendering.PostProcessing;
using System;
using System.Collections.Generic;
using TMPro;
public class GameManager : Singleton<GameManager> {
private PostProcessProfile _postProcessingProfile;
private Vignette _vignette;
[SerializeField] private GameObject _queueItemPrefab;
private Transform _trainingQueue;
private readonly int _queueMax = 5;
private readonly int _queueNumberMax = 5;
private List<QueueItem> _currentlyQueued;
private List<GameObject> _currentlyQueuedItems;
private void Awake()
{
_postProcessingProfile = GameObject.Find("PostProcessingVolume").GetComponent<PostProcessVolume>().profile;
_vignette = _postProcessingProfile.GetSetting<Vignette>();
_vignette.active = false;
_trainingQueue = GameObject.Find("TrainingQueue").transform;
_currentlyQueuedItems = new List<GameObject>();
UpdateQueueItemLists();
}
public void DeQueueTroop(GameObject textObject)
{
TMP_Text text = textObject.GetComponent<TMP_Text>();
GameObject parentGameObject = textObject.transform.parent.gameObject;
if (Convert.ToInt32(text.text) <= 1)
{
// There's nothing in the list, Output: Count: 0
print("Count: " + _currentlyQueuedItems.Count);
_currentlyQueuedItems.Remove(parentGameObject);
for (int i = 0; i < _currentlyQueuedItems.Count; i++)
{
UpdatePosition(_currentlyQueuedItems*, i);*
}
Destroy(parentGameObject);
UpdateQueueItemLists();
}
-
else*
-
{*
parentGameObject.GetComponent().DeQueue();
-
}*
- }*
public void QueueTroop(string troopType)
- {*
foreach (QueueItem current in _currentlyQueued)
{
if (Convert.ToInt32(current.GetText().text) < _queueMax)
{
if (current.GetUnitType() == troopType)
{
current.Queue();
return;
}
}
}
if (_currentlyQueuedItems.Count < _queueNumberMax)
{
GameObject queueItem = Instantiate(_queueItemPrefab, _trainingQueue, false);
UpdatePosition(queueItem, _currentlyQueuedItems.Count);
_currentlyQueuedItems.Add(queueItem);
int indexOfItem = _currentlyQueuedItems.IndexOf(queueItem);
UpdateQueueItemLists();
_currentlyQueued[indexOfItem].SetUnitType(troopType);
}
}
private void UpdateQueueItemLists()
{
if (_currentlyQueuedItems == null) return;
List old = _currentlyQueuedItems;
_currentlyQueuedItems = new List();
foreach (GameObject go in old)
-
{*
if (go != null)
-
{*
_currentlyQueuedItems.Add(go);
-
}*
-
}*
_currentlyQueued = new List(_currentlyQueuedItems.Count);
for (int i = 0; i < _currentlyQueuedItems.Count; i++)
{
if (currentlyQueuedItems*)
_currentlyQueued.Add(_currentlyQueuedItems.GetComponent());*
}
}_
private void UpdatePosition(GameObject go, int idx)
* {*
// Don’t touch these values! They are these because it spawns in world space and I can’t change that
go.transform.position = new Vector3(17.5f - (2 * idx), -8.5f, 0f);
* }*
}
Image of the output of the print(_allQueueItems.Count); call:
[187034-scuffedlists.png|187034]