Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
1 capture
11 Jun 22 - 11 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by Inan-Evin · Aug 01, 2015 at 03:41 PM · c#functionparameterspass

How to Skip Parameter in C#

Hello everyone, I need some information about function parameters in C#. Here is my situation: I have a singleton script called SubtitleManager. This script has a lot of other classes that are inherited from this, and has 1 main function to print subtitles to the screen. Most of the subtitles are the voices of the main character, but there are some other character's subtitles. I want these character's subtitles to be in different color than the main character's. Since I have one main function to print subtitles, I used parameters.

 protected void BypassSubtitle(string text, float duration, bool differentColor, Color newColor)

As you can see, we have a boolean variable, script changes the color of the subtitle to newColor variable, or default Color.white variable according to that boolean.

My question is, in some scripts, when I don't want to change the subtitle color, I do not want to pass the last 2 parameters while calling the function. When I write:

 BypassSubtitle("blabla", 3.0f);

I get errors that say the function can't take two parameters, as it is obvious that I have 4 parameters so I have to do:

 BypassSubtitle("blabla", 3.0f, false, Color.white);

So over here, eventhough I won't be changing the color to another color, I still have to pass any Color parameter for the function to work, I am curious, can this cause overheads? Because actually I have 7-8 parameters, which are situation dependant, and also I have AudioManager which uses parameters too. So I though passing unnecessary parameters can cause performance issues, but I don't know how not to call the function without the parameters that won't be used. Any ideas? Thanks :)

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Dave-Carlile · Aug 01, 2015 at 03:49 PM

There are a couple of ways you can do this.

Default parameters:

  protected void BypassSubtitle(
      string text, float duration, 
      bool differentColor = false, Color newColor = Color.white);

  // edit: it turns out that you can't use Color.white or any of the other color
  // properties because they aren't constants that can be determined at compile time,
  // so you must use the default keyword instead...
  // the difference is that the default color will be black instead of white
  protected void BypassSubtitle(
      string text, float duration, 
      bool differentColor = false, Color newColor = default(Color));


Specifying the default value allows you to not pass the parameter.

Another way is to overload the method. You start with the one that takes all of the parameters, and in your overload you call that one, passing in defaults for the values that you don't have...

 // function with all the params
 protected void BypassSubtitle(string text, float duration, bool differentColor, Color newColor)

 // function with some of them
 protected void BypassSubtitle(string text, float duration)
 {
   BypassSubtitle(text, duration, false, Color.white);
 }


 protected void BypassSubtitle(string text)
 {
   BypassSubtitle(text, 1.5f, true, Color.green);
 }
 

If you go with the overloading, each overload must have a unique call signature, meaning it must be a unique combination of data types/parameter order.

Comment
Add comment · Show 10 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Inan-Evin · Aug 01, 2015 at 04:08 PM 0
Share

Well I'd like to go with writing default parameters, that sounds really convenient but when I try it, I have this error :

The expression being assigned to optional parameter "newColor" must be a constant or a default value.

Seems like there is no problem with the boolean but something wrong with the Color.white being default?

avatar image Dave-Carlile · Aug 01, 2015 at 05:07 PM 1
Share

Hmm, I guess this is what Color.white looks like...

 public static Color white
 {
     get
     {
         return new Color(1f, 1f, 1f, 1f);
     }
 }

So it's not a compile time constant.

You can also use the default keyword when declaring the method...

 protected void BypassSubtitle(
       string text, float duration, 
       bool differentColor = false, Color newColor = default(Color));


But the color will be black then since the default is all 0s.

avatar image Inan-Evin · Aug 01, 2015 at 06:48 PM 0
Share

Thank you sir!

avatar image impurekind · Oct 23, 2018 at 09:08 AM 0
Share

Really simply, I'm using the following check:

 if (Physics.Linecast(transform.position, player.transform.position))
         {
             canHitPlayer = true;
         }
         else
         {
             canHitPlayer = false;
         }

But I also want to tell it to check the queryTriggerInteraction part of the function so it checks the triggers of any objects too, this is the last of the four parameters, while basically ignoring the third parameter, the layer mask, that I don't care about because I don't want to ignore/set any layers.

How would I write it to do that?

avatar image Hellium impurekind · Oct 23, 2018 at 09:27 AM 1
Share
 Physics.Linecast(transform.position, player.transform.position, queryTriggerInteraction: QueryTriggerInteraction.Collide)
avatar image impurekind Hellium · Oct 23, 2018 at 01:30 PM 0
Share

That's co$$anonymous$$g up with a squiggly error line under the Lincast word in Visual Studio, and when I mouse over it, it says "No overload for method 'Linecast' takes 3 arguments."

Ideas?

Show more comments
avatar image
1

Answer by felix11 · Aug 01, 2015 at 08:37 PM

Sorry, that didn't work, let me try again:

 public Class YourClass {
 protected void BypassSubtitle(string text, float duration, Color newColor) 
 {
  //Code1 
 } 
 protected void BypassSubtitle(string text, float duration) 
 { 
 //Code2 
 }
     
 //then you can just do
    BypassSubtitle ("Text", 1.2f, color.red);/* (this will execute Code1 with the Parameters)*/
          //or
          BypassSubtitle ("Text", 1.2f);/*(this will execute code2) /*
 }

You pass in you Code at Code1 and Code2 and it will decide which code to execute based on the Amount and Type of Parameter you've give into it. You can in Theory have infinite of those Methods with the same name but other Parameters. (This is called Overloading a Method) Hope this helps, its my first post :D

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

26 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

calling an void from another script and passing paramters... 1 Answer

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Can't Set Animator Boolean Parameter C# 1 Answer

Script will be found on the client but not on the server. 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges