Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
This question was closed Oct 13, 2021 at 02:32 AM by SamyBoyJim for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by SamyBoyJim · Oct 03, 2021 at 02:56 PM · 2dunity 2dlistscount

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: alt text

scuffedlists.png (42.3 kB)
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

  • Sort: 
avatar image
0
Best Answer

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);
     }


Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by TalkingBlah · Oct 03, 2021 at 04:46 PM

_currentlyQueuedItems = new List(); this line of code you use doesnot populate the _currentlyQueuedItems list

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image SamyBoyJim · Oct 03, 2021 at 10:37 PM 0
Share

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

avatar image TalkingBlah SamyBoyJim · Oct 04, 2021 at 10:57 AM 0
Share

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

avatar image SamyBoyJim TalkingBlah · Oct 04, 2021 at 11:05 PM 0
Share

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

Answers Answers and Comments

294 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges