- Home /
Button Listener void calling all prefab buttons in scene.
Hello, I've got an issue. I'm building an Inventory. Once the player has picked up an item the item is added to the UI inventory wrapper. The buttons are prefabs. The issue is once an inventory item is clicked all of the buttons are called on click.
For instance if there is more than one 9mm pistol in the players inventory and the player clicks on one of the 9mm Pistols to equip it then all of the other 9mm pistols within the inventory is called as if the player clicked on all the buttons at once.
Here is the equip script.
public void EquipItem()
{
if(this.name.Contains("(WEP)"))
{
foreach(Weapons i in gm.GetComponent<inventoryMaster>().playerWeapons)
{
if(i.WeaponName == this.name)
{
if(i.handed == 1)
{
if(gm.GetComponent<inventoryMaster>().currentWeapon != null)
{
Destroy(gm.GetComponent<inventoryMaster>().currentWeapon);
equipedIcon.SetActive(false);
return;
}
else
{
GameObject equipedWep = Instantiate(i.weaponObj, oneHandedPoint.transform.position, oneHandedPoint.transform.rotation) as GameObject;
equipedWep.transform.SetParent(oneHandedPoint.transform);
equipedWep.transform.localScale = new Vector3(0.0007f, 0.0007f, 0.0007f);
gm.GetComponent<inventoryMaster>().currentWeapon = equipedWep;
equipedIcon.SetActive(true);
}
}
}
}
}
}
Thank you for any help on this.
Answer by bpaynom · Feb 01, 2019 at 03:26 PM
At the end of if(i.WeaponName == this.name)
(before closing the area of code with } ) put a break; line. BTW, I don't think your code is the best way to achieve what you are trying to do.
That worked, Thank you. Not sure why I didn't think of that!
The way I'm going about it is working, but I would imagine it could cause bugs or errors in the future? Could you recommend anything?
Even though I've got a few years of experience in Unity I'm primarily a Back-End Web Dev so I'm still learning as I go.
I'll probably just keep pushing forward and see how it goes as more is added.
Thank you for your help.