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

161
Views
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 answers
Answer question

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