I have a script issue I can’t quite figure out where it is going wrong. I am trying to reach out to my List of turrets on my player object, grab the itemID, and populate it in each of my equipment panel slots. The issue is that it skips over the very first turret itemID and populates the rest of them.
In this example with the code below I have 4 turrets in a weapTurrets List. In that list, starting at Index 0 the ItemIDs are;
3, 2, 1, 1
In each of my slots they are numbered as;
0, 2, 1, 1
So it skips that first entry of 3 while populating the rest in the appropriate slot order. The below code is in my Update() function
for (int w = 0; w < player.weapTurrets.Count; w++)
{
slots[w].transform.GetComponent<EquipmentSlot>().itemID = player.weapTurrets[w].itemID;
}
If anyone has any ideas Id appreciate the advise, thanks!
Unfortunately it is a List instead of an Array so length won’t work. Though Count should be the equivalent of length when it comes to List as far as I know.
I would add a simple debug first, to very your 3,2,1,1 is really being added. This would alreast help you under stand what is being past around. If you have null being passed in? or maybe it’s in reverse order, maybe some where else the first entry is not being set, or it’s getting changed with you knowing. We don’t have the full code so all we can do is guess.
but, your code looks correct.
Debug.log("Set " + w + " to be " + player.weapTurrets[w].itemID );
I first wanted to make sure I wasn’t going crazy and missed something obvious in my code. The debug.Log is a good idea though now the mystery deepens.
Putting that Debug in now prints out;
Set 0 to be 3
Set 1 to be 2
Set 2 to be 1
Set 3 to be 1
So it looks like its getting that 3 correctly through the script but the ItemID value is still set to 0. Something somewhere is overriding the value just for that one slot but not the others, but at least it narrowed it down to find out that the piece of code is working.
Post more of the code if possible. Something is changing it. Post any code that deals with any lines of code that change the itemID at any time or things that change the slot array or make new slots.
I narrowed down the issue finally to a further bit of my original code that I thought was acting independently but I really didn’t think it through fully…my bad… But now I am not fully sure how to handle this one.
I have 3 sets of Equipment Slots, each of these have the EquipmentSlot script on them with an index that has already been numbered…Weapon Slots which are indexed 0 through 7, System Slots that are indexed 8 through 15, and Armor Slots that are indexed 16 through 23.
On my player controller these are three separate lists, weapTurrets, sysTurrets, and armTurrets…So for the commented out code below which is where my mind went south in thinking, i need to figure out how to properly handle getting the info from all three of these lists into there proper spot
public void PopulateEquipment()
{
for (int w = 0; w < player.weapTurrets.Count; w++)
{
int id = player.weapTurrets[w].itemID;
slots[w].transform.GetComponent<EquipmentSlot>().itemID = id;
}
/* for (int y = 0; y < player.sysTurrets.Count; y++)
{
slots[y].transform.GetComponent<EquipmentSlot>().itemID = player.sysTurrets[y].itemID;
}
for (int z = 0; z < player.armTurrets.Count; z++)
{
slots[z].transform.GetComponent<EquipmentSlot>().itemID = player.armTurrets[z].itemID;
} */
}
In the commented out code, can you simply add offsets when you access slots[y] and slots[z]? For example:
public void PopulateEquipment()
{
for (int w = 0; w < player.weapTurrets.Count; w++)
{
int id = player.weapTurrets[w].itemID;
slots[w].transform.GetComponent<EquipmentSlot>().itemID = id;
}
int sysOffset = player.weapTurrets.Count;
for (int y = 0; y < player.sysTurrets.Count; y++)
{
slots[y + sysOffset].transform.GetComponent<EquipmentSlot>().itemID = player.sysTurrets[y].itemID;
}
int armOffset = sysOffset + player.sysTurrets.Count;
for (int z = 0; z < player.armTurrets.Count; z++)
{
slots[z + armOffset].transform.GetComponent<EquipmentSlot>().itemID = player.armTurrets[z].itemID;
}
}
That is a much simpler way to do it then the direction I was heading, you just saved me several dozens of line of code! Thank you very much for helping me solve that one, it will also be useful in other parts where I will need to do a similar structure.