Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

192
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda