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

162
Visualizações
C# - Unity - Problem with Parameter passing

I'm trying to extract a lot of code in my program and I encountered a problem.

This is my function to extract a smelting process:

public IEnumerator Smelting(Button button, Image bar, BigDouble material, BigDouble output)
    {
        button.interactable = false;

        float t = 0f;
        while (t <= 1f)
        {
            bar.fillAmount = t;
            t += Time.deltaTime / 4;
            yield return null;
        }

        bar.fillAmount = 0;
        //saveLoadHandler.data.copperBar += smeltingHandler.smeltCopperOreOutput;
        material += output;
        button.interactable = true;
    }

My Problem is now that I want to add the output to the material with: material += output. The Command above shows what these parameter look like.

So it looks like in this case the material doesn't get assigned correctly.

I don't want to go the way if(type = copper){copper += output } else if(type = iron)...

Is there a workaround for that?

Thanks in advance!

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

if BigDouble is just like a normal double, it is a value type, and is therefore being passed by value to your method. You can add the ref keyword to pass it by reference:

public IEnumerator Smelting(Button button, Image bar, ref BigDouble material, BigDouble output)

However, iterators cannot have ref arguments, so another workaround might be to pass a delegate to the method who's job it is to increment something

public IEnumerator Smelting(Button button, Image bar, Action<BigDouble> incrementMaterial, BigDouble output) {
    ....
    incrementMaterial(output); // instead of material += output
    ....
}

and then the caller:

StartCoroutine(smeltingCore.Smelting(
     smeltCopperOreButton, 
     smeltCopperOreBar, 
     amount => saveLoadHandler.data.copperBar += amount, 
     smeltingHandler.smeltCopperOreOutput));

Of course, at this point you're only passing in output to use it in the delegate so you can simplify both:

public IEnumerator Smelting(Button button, Image bar, Action incrementMaterial) {
    ....
    incrementMaterial(); 
    ....
}

and

StartCoroutine(smeltingCore.Smelting(
     smeltCopperOreButton, 
     smeltCopperOreBar, 
     () => saveLoadHandler.data.copperBar += smeltingHandler.smeltCopperOreOutput 
     ));
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