Rumata
Rumata

Reputation: 1047

How to call a function without passing all parameters to it?

Unity shows an error in this case. how can I mark these parameters as "optional", or is there any better solution?

Example:

void example(Texture texture1, Texture texture2, Texture texture3, Texture texture4) {

  renderer1.material.mainTexture = texture1;

  if (texture2 != null) {
    renderer2.material.mainTexture = texture2;
  }
  if (texture3 != null) {
    renderer3.material.mainTexture = texture3;
  }
  if (texture4 != null) {
    renderer4.material.mainTexture = texture4;
  }

}

    public void callExample(){
    example(texture1, texture2);
};

Upvotes: 1

Views: 738

Answers (1)

Alex
Alex

Reputation: 583

To make argument optional you should provide default value, just like this:

void example(Texture texture1, Texture texture2 = null, Texture texture3 = null, Texture texture4 = null) {

Upvotes: 2

Related Questions