Whenever I call the Instantiate function it duplicates every single function with it and doubles every number
private Button[] Checks;
public GameObject tni;//TextAndInput
public int Coins;
public TMPro.TextMeshProUGUI Score;
private void Start()
{
Checks = GetComponentsInChildren<Button>();
foreach(Button button in Checks)
{
button.onClick.AddListener(Add);
}
}
private void Update()
{
Score.SetText(Coins.ToString());
if (Input.GetKeyDown(KeyCode.Space))
{
Duplicate();
}
}
public void Add()
{
Coins++;
}
public void Duplicate()
{
GameObject duplicate = GameObject.Instantiate(tni);
duplicate.transform.position=new Vector3(tni.transform.position.x, tni.transform.position.y-100, tni.transform.position.z);
duplicate.transform.SetParent(GameObject.FindGameObjectWithTag("Canvas").transform, false);
Button ButtonDuplicate = GetComponentInChildren<Button>();
ButtonDuplicate.onClick.AddListener( () => Add() );
}
** For example, the add function adds 2 on the first button and 1 on the duplicated button. and when I duplicate the buttons it will duplicate first the 1 button then when If I duplicate again it will duplicate another 2 so now they are 4 buttons and If I duplicate them again they will be 8 and so on If you know how to fix it please tell me **