How to find certain TextMeshProUGUI if multiple are attached?
I have an inventory with six slots. Each slot has three TextMeshProUGUI attached: singleGoldAmount, stackSize, resultingGoldAmount. These TextMeshProUGUI are tagged with names corresponding to their purpose.
I want the inventory in Start() to find each TextMeshProUGUI-Element and put them in a list like private TextMeshProUGUI [] singleGoldAmountText, vice versa for stacksize and resultingGoldAmount. I've tried FindWithTag, but didn't get the solution I was looking for.
I've tried to solve this for two days but I feel like I'm not making any progress. By now, I'm probably blind to the obvious solution and too stupid to abstract from this particular problem.
This is as far as I came: using System.Collections.Generic; using System.Collections; using UnityEngine; using System; using TMPro; using UnityEngine.Serialization;
public class Inventory : ItemContainer
{
[SerializeField] List<Item> startingItems;
[SerializeField] Transform itemsParent;
// Blubb testing stuff here
[SerializeField] List<Werkstatt_Slot> werkstattSlot;
[SerializeField] Transform werkstattSlotTrans;
// Blubb ending testing
public string Goldwert;
public void Start()
{
SetStartingItems();
Init();
}
private void Init()
{
for (int i = 0; i < werkstattSlot.Count; i++)
{
// ?? I'm fairly sure I need this to fill the list?
}
}
private void OnValidate ()
{
// GoldWertFeld = TextMeshProUGUI.FindGameObjectsWithTag("Goldwert"); // NOPE NOT WORKING
if (itemsParent != null)
{
itemSlots = itemsParent.GetComponentsInChildren<ItemSlot>(); //Works like a charm
werkstattSlot = werkstattSlot.GetComponentsInChildren<Werkstatt_Slot>(); //Does not work
// itemWert = itemsParent.GetComponentsInChildren<TextMeshProUGUI>(); //Works, but only as long as there is only ONE type of TextMeshProUGUI... I have three now.
// itemWert = werkstattSlotGO.FindWithTag("Goldwert").GetComponentsInChildren<TextMeshProUGUI>(); //This was another try. Didn't work.
// itemWert = GameObject.FindWithTag("Goldwert").GetComponentsInChildren<TextMeshProUGUI>(); // This one finds a single entry.
// itemWert = GoldWertFeld.GetComponent<TextMeshProUGUI>(); // Nope
}
SetStartingItems();
Init();
}
private void SetStartingItems()
{
int i = 0;
for (; i < startingItems.Count && i<itemSlots.Length; i++)
{
itemSlots[i].Item = startingItems[i].GetCopy();
}
for (; i<itemSlots.Length; i++)
{
itemSlots[i].Item = null;
}
}
}
Could somebody help me out? Pointing me to a tutorial would be fine, too, I am just really stuck in a circle here.
Your answer
Follow this Question
Related Questions
Navmesh, help setting position to nearest tag? (Javacript) 0 Answers
How to use 2 tags in FindGameObjectsWithTag. 2 Answers
Turn on/off renderer for objects found with "FindGameObjectsWithTag" 1 Answer
How do I use arrays, with getcomponent? 2 Answers
Check if there's no GameObject with specific tag in the field (in the camera view) 0 Answers