- Home /
The question is answered, right answer was accepted
Lists are empty when they aren't supposed to be
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], i);
}
Destroy(parentGameObject);
UpdateQueueItemLists();
}
else
{
parentGameObject.GetComponent<QueueItem>().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<GameObject> old = _currentlyQueuedItems;
_currentlyQueuedItems = new List<GameObject>();
foreach (GameObject go in old)
{
if (go != null)
{
_currentlyQueuedItems.Add(go);
}
}
_currentlyQueued = new List<QueueItem>(_currentlyQueuedItems.Count);
for (int i = 0; i < _currentlyQueuedItems.Count; i++)
{
if (_currentlyQueuedItems[i])
_currentlyQueued.Add(_currentlyQueuedItems[i].GetComponent<QueueItem>());
}
}
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:
Answer by SamyBoyJim · Oct 13, 2021 at 02:30 AM
I fixed the problem by just re writing the code and using arrays. Here is the code if anyone is interested:
private void Awake()
{
_trainingQueue = GameObject.Find("TrainingQueue").transform;
_currentlyQueuedItems = new QueueItem[_maxQueueItemObjects];
for (int i = 0; i < _currentlyQueuedItems.Length; i++)
{
_currentlyQueuedItems[i] = Instantiate(GameAssets.I.QueueItemPrefab, _trainingQueue, false).GetComponent<QueueItem>();
_currentlyQueuedItems[i].transform.localPosition = new Vector3(430 - (i * 120), 0f, 0f);
_currentlyQueuedItems[i].gameObject.SetActive(false);
}
}
public void DeQueueTroop(GameObject senderObject)
{
QueueItem item = senderObject.GetComponent<QueueItem>();
AddCost(item.GetUnitType());
if (item.GetNumberCurrentlyQueued() <= 1)
{
item.SetUnitType("Null");
item.ResetNumberCurrentlyQueued();
item.gameObject.SetActive(false);
}
else
{
item.DeQueue();
}
}
public void QueueTroop(string troopType)
{
foreach (QueueItem item in _currentlyQueuedItems)
{
if (!item.gameObject.activeSelf) continue;
if (item.GetNumberCurrentlyQueued() < _queueNumberMax)
{
if (item.GetUnitType() == troopType)
{
SubtractCost(troopType);
item.Queue();
return;
}
}
}
foreach (QueueItem item in _currentlyQueuedItems)
{
if (!item.gameObject.activeSelf)
{
item.gameObject.SetActive(true);
item.ResetHazeFill();
item.Queue();
item.SetUnitType(troopType);
SubtractCost(troopType);
return;
}
}
}
private void SubtractCost(string troopType)
{
if (troopType == "Soldier")
PlayerStats.IncrementGold(-costSoldiers);
else if (troopType == "Ship")
PlayerStats.IncrementGold(-costShips);
else if (troopType == "Plane")
PlayerStats.IncrementGold(-costPlanes);
}
private void AddCost(string troopType)
{
if (troopType == "Soldier")
PlayerStats.IncrementGold(costSoldiers);
else if (troopType == "Ship")
PlayerStats.IncrementGold(costShips);
else if (troopType == "Plane")
PlayerStats.IncrementGold(costPlanes);
}
Answer by TalkingBlah · Oct 03, 2021 at 04:46 PM
_currentlyQueuedItems = new List(); this line of code you use doesnot populate the _currentlyQueuedItems list
The foreach loop afterwards does populate it with non-null gameobjects but it doesn't seem to be detecting that some of the objects do not exist and does populates the list with non-null gameobjects
for (int i = 0; i < _currentlyQueuedItems.Count; i++) the time when this loop is created the value of currebtlyQuedItems is 0 that's why it's not working probably
I probably should have said this in my post but I know that :). The problem is that I don't know what is causing the count to be 0 because in the inspector it is saying that it has 1 entry.
Follow this Question
Related Questions
Player Movement doesn't work, but Debug.Log shows that it should 1 Answer
How do I manage multiple colliders on a gameObject? 1 Answer
Tilemap Collider not working after build on android 0 Answers
2D URP: Determine amount of light hitting object or specific point in scene 0 Answers
convert unity versions, 0 Answers