For Loop Problem
Hey, So im making an RPG where u can pick up weapons and then use that one equipped weapon to strike and so on. For that im saving every weapon avaiable in an Array and just set one item active on the player when that one weapon is drawn.
To find the weapon, that should be equipped and therefore set active, a string is passed from one script, that holds the excat weapon name, to the SelectWeapon script which contains the structure above explained.
Everything works just fine, however I just added one more Weapon to the list, and when i now want to equip that, a second Weapon in the list is also set active. So, when I press 1, not only the desired weapon is set active, but also the item at array[1]. I dont know why it suddenly acts in that behaviour. Here is the Code. Thanks in advance.
//CODE STARTS HERE .
public GameObject[] wl_Col_H;
public GameObject[] wl_Row_C;
public static string eqWeaponHand;
public static string eqWeaponChar;
if(Input.GetKeyDown(KeyCode.Alpha1)) {
for (int j = 0; j < wl_Row_C.Length; j++)
{
wl_Row_C[j].SetActive(false);
if(wl_Row_C[j].name == eqWeaponChar)
{
wl_Row_C[j].SetActive(true);
break;
}
}
It's not clear what your logic is intended to do, but I'm pretty sure it's wrong. You first deactivate the object (implying that you only want the correctly-named object to be active), but then having found the correctly-named object you break out of the loop. So any objects after the (first) correctly-named one won't be checked (and won't be activated or deactivated).
Exactly. I answered something like that a while ago but I deleted it since he said he already got it working.
Answer by TheItCrOw · Feb 02, 2018 at 01:50 PM
Nvm I got it working. It was a stupid mistake, I set that one item at Array[1] somewhere else to always set active when 1 is pressed. The code above works just fine for the explained behaviour.