- Home /
Button Action Triggering Multiple Times Per Click?
I'm working on an RTS. I have a UI setup where the OnClick() functions get swapped out based on what object is selected. Right now I have a Monastery Building(Town Hall/Hub) and a Initiate Unit(Civilian/Villager). When I have the Monastery selected, the first action button is assigned to add Initiates to a spawning queue. However when I click on it, it adds anywhere from 4-6 Initiates to the queue. I only want it to trigger once per click. Anyone know what's going on? Or if you can point me in the direction of some resources for this I appreciate it!
This Is Part of my Player controller Script:
//This is my script for handling Left Click Events
void LeftClick()
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast(ray, out hit, Mathf.Infinity))
{
if(hit.collider.tag == "Ground" && Input.mousePosition.x <= Screen.width - 180)
{
infoPanel.alpha = 0;
infoPanel.blocksRaycasts = false;
infoPanel.interactable = false;
selectedObject = null;
objectInfo.isSelected = false;
objectInfo = null;
}else if(hit.collider.tag == "SelectableObject")
{
selectedObject = hit.collider.gameObject;
objectInfo = selectedObject.GetComponent<WorldObject>();
objectInfo.isSelected = true;
infoPanel.alpha = 1;
infoPanel.interactable = true;
infoPanel.blocksRaycasts = true;
//This Is the Part that adds the action to the button
Action1 = Button1;
Button btn1 = action1.GetComponent<Button>();
btn1.onClick.AddListener(Action1);
}
}
}
This Gets the Action:
//This Retrieves the button action from the selected object's action list
void Button1()
{
objectInfo.ObjectAction1();
}
This is the actual button action:
public override void ObjectAction1()
{
//Checks to see if the player has enough resources
if(initiate.popCost < (GM.maxPop-GM.pop) && initiate.foodCost < GM.food && initiate.woodCost < GM.wood && initiate.stoneCost < GM.stone && initiate.goldCost < GM.gold)
{
//Adds to the spawn queue and subtracts the cost
itemsInQueue++;
GM.food -= initiate.foodCost;
GM.wood -= initiate.woodCost;
GM.stone -= initiate.stoneCost;
GM.gold -= initiate.goldCost;
}
}
Any Help is greatly appreciated!
Answer by Nixmortem · Apr 06, 2018 at 08:57 PM
Fixed it! I think I was adding the function twice. So I got rid of the Button1 Method and changed my code:
//Old Code
Action1 = Button1;
Button btn1 = action1.GetComponent<Button>();
btn1.onClick.AddListener(Action1);
//New Code
Action1 = objectInfo.ObjectAction1;
action1.onClick.AddListener(Action1);