Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

85
Views
Duplicating functions

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 **

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

The problem line seems to be

Button ButtonDuplicate = GetComponentInChildren<Button>();

you always do this on the very same object (the one this script is attached to) so

ButtonDuplicate.onClick.AddListener( () => Add() );

always adds an additional callback to Add for the first button that is found among the children of this object.


It seems to me you rather wanted to do

Button ButtonDuplicate = duplicate.GetComponentInChildren<Button>();

in order to get the component of the button you just spawned instead.


Update

To your comment about the position

In

duplicate.transform.position=new Vector3(tni.transform.position.x, tni.transform.position.y-100, tni.transform.position.z);

you always take the same tni position which is never changed and from there go -100 in Y.

Instead rather store the last spawned position e.g. like

private Vector3 currentSpawnPos;

private void Start ()
{
    ...

    currentSpawnPos = tni.transform.position - Vector3.up * 100;
}

public void Duplicate()
{
    ...
    duplicate.transform.position=new Vector3(currentSpawnPos);
    currentSpawnPos -= Vector3.up * 100;
    ...
}

Or actually instead of calculating that on your own you could use a VerticalLayoutGroup and just add your buttons and they will be automatically be placed below each other

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!