Hello, everyone!
This has been happening since last night and I haven’t solved it yet. I’ve searched and experimented with creating a local variable inside the loop, or even creating two coroutine functions that create different buttons, but neither worked.
public void InitSlotless(GameObject elementPreFab, List<Sprite> spriteList, List<PCBase> fromDB)
{
ClearGrid();
StartCoroutine(MakeChild(elementPreFab, spriteList, fromDB));
}
public void InitSlot(GameObject elementPreFab, List<Sprite> spriteList,
List<PCBase> fromDB, List<KeyValuePair<bool, int>> clothOrRigSlot)
{
ClearGrid();
StartCoroutine(MakeChildren(elementPreFab, spriteList, fromDB, clothOrRigSlot));
}
IEnumerator MakeChildren(GameObject elementPreFab, List<Sprite> spriteList,
List<PCBase> fromDB)
{
for (int i = 0; i < spriteList.Count; ++i)
{
GameObject go = Instantiate(elementPreFab, _scrollView.content.transform, false);
go.name = $"{i}";
int index = i;
go.GetComponent<ItemChildData>().Initialize(index, fromDB[i]);
SelectOrDeselectSlotlessItem(go);
ObjectTypeButton btn = go.GetComponentInChildren<ObjectTypeButton>();
btn.GetComponentInChildren<ObjectTypeButton>().PressEndEvent.AddListener(() =>
ClickItem(this, index));
yield return null;
}
}
IEnumerator MakeChildren(GameObject elementPreFab, List<Sprite> spriteList,
List<PCBase> fromDB, List<KeyValuePair<bool, int>> clothOrRigSlot)
{
for (int i = 0; i < spriteList.Count; ++i)
{
GameObject go = Instantiate(elementPreFab, _scrollView.content.transform, false);
go.name = $"{i}";
int index = i;
go.GetComponent<ItemChildData>().Initialize(index, fromDB[i], clothOrRigSlot[i]);
SelectOrDeselectItem(go);
ObjectTypeButton btn = go.GetComponentInChildren<ObjectTypeButton>();
btn.GetComponentInChildren<ObjectTypeButton>().PressEndEvent.AddListener(() =>
ClickItem(index));
yield return null;
}
}
public void ClickItem(GridViwer gird, int index)
{
Debug.Log($"{gird.name}, {index}@@@");
gridItemClickEvent.Invoke(gird, index);
}
public void ClickItem(int index)
{
Debug.Log($"{this.name}, {index}!!!");
gridItemClickEvent.Invoke(this, index);
}
After writing the code above (I’m experimenting, so there are some inefficiencies), I pressed the btn that I thought was connected to ‘ClickItem(index)’ and looked at the output log. The log shows that it was taken from ‘ClickItem(GridViewer gird, int index)’. Also, the index value is different from the actual order of the btn.
I don’t understand why a different function is connected to the btn than the function ‘ClickItem(index)’ in the code. I also don’t understand why the order value I get from the local variable ‘int index = i’ doesn’t work either…
If you have any solution or clue about this phenomenon, I would really appreciate it if you could share it.