- Home /
The question is answered, right answer was accepted
A few problems with my weapon wheel script
So i made a nice weaponwheel script and upgraded my weapon manager to fit it...however every time i call the Debug Weapons function...all the weapons disappear although i call the SetWeapon funtion at the end, which should reenable the weapons....but it doesn't....the strange thing is that....if i call the SetWeapon function anytime after the DebugWeapons function.....the weapons are reenabled....im clueless...here are both of the scripts
Here is the relevant part of the WeaponsManager script....
public void DebugWeapons()
{
foreach(InventoryWeapon weapon in inventory)
{
if(weapon.colliders == null)
return;
else
{
weapon.colliders.gameObject.SetActive(false);
weapon.weaponcode = Array.FindIndex(inventory, inventory => inventory == weapon);
}
}
weapons[currentweapon].colliders.gameObject.SetActive(true);
SetWeapon(currentweapon);
}
public void SetWeapon(int weaponcode)
{
if(weaponcode < 0)
{
weaponcode = noofweapons - 1;
}
else if(weaponcode > noofweapons - 1)
{
weaponcode = 0;
}
currentweapon = weaponcode;
lastweapon = weaponcode;
InventoryWeapon weapon = weapons[weaponcode];
if(weapon == null)
{
Debug.LogWarning("There's no weapon with the code " + weaponcode);
return;
}
if(!weapon.locked)
{
foreach(InventoryWeapon weaponcheck in weapons)
{
if(weaponcheck.colliders == null)
{
return;
}
else
{
weaponcheck.colliders.maincollider.GetComponent<WeaponCollision>().enemies.Clear();
weaponcheck.colliders.secondarycollider.GetComponent<WeaponCollision>().enemies.Clear();
if(weaponcheck.weaponcode == weaponcode)
{
weaponcheck.colliders.gameObject.SetActive(true);
}
else
{
weaponcheck.colliders.gameObject.SetActive(false);
}
}
}
weapondata = weapon.weapon;
combat.wcolliders = weapon.colliders;
combat.attackrate = weapondata.attackrate;
combat.mindamage = weapondata.mindamage;
combat.maxdamage = weapondata.maxdamage;
combat.currentweapon = weapondata.name;
combat.damagerange = weapondata.damagerange;
}
else
{
if(lastscroll > 0)
{
currentweapon += 1;
}
else if(lastscroll < 0)
{
currentweapon -= 1;
}
}
}
}
Any and all help is appreciated...Thanks in advance!
It's an urgent matter so can anybody please look into this?.....i think it's an effect of the foreach loop running even after executing the SetWeapons() function......how do i correct it? @Hellium, sorry to disturb you but can you help?
@Devster2020 im sorry to disturb like this but i need some urgent help so can you look into this question?
Hi @rage_co I'm here.. I'm going to read the topic
Answer by Devster2020 · Jul 06, 2021 at 09:27 AM
I'm reporting the answer here, to let users find it quickly..
The problem in the script is the "return" key that block the loop when a weapon is unavailable..
To resolve this, we need to change the "return" key with the "continue" key to skip the unavailable weapon and continue the loop with the next one..
Here the code:
public void DebugWeapons() {
foreach(InventoryWeapon weapon in inventory) {
if(weapon.colliders == null)
continue; // Using "continue" instead of "return" allows you to skip the unavailable weapon and continue the cycle with the next weapon
else {
weapon.colliders.gameObject.SetActive(false);
weapon.weaponcode = Array.FindIndex(inventory, inventory => inventory == weapon);
}
}
weapons[currentweapon].colliders.gameObject.SetActive(true);
SetWeapon(currentweapon);
}
Follow this Question
Related Questions
How do I stop my weapon from firing while reloading? 2 Answers
Cycling Weapons, using C# int string problem 1 Answer
Weapon shooting mechanism problem. 1 Answer
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers