Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 Demigiant · Jul 10, 2014 at 04:18 PM · c#overload

Method overloading fails with implicit typecasting and optional parameters

Hi,

I posted an "opening" to this question earlier and thanks to tswalk I realized one very huge weirdness, related to Unity and method overloading.

I encountered an issue while creating method overloads. This compiles without problems (EXAMPLE A):

 void Test(Color v, bool b)
 {
   Debug.Log("color");
 }
 void Test(Vector4 v, bool b)
 {
   Debug.Log("vector4");
 }
 
 void Start()
 {
   Test(Color.red, true);
 }

While this generates an ambiguous call error (EXAMPLE B) (same as above, but the Test methods have optional parameters):

 void Test(Color v, bool b = true)
 {
   Debug.Log("color");
 }
 void Test(Vector4 v, bool b = true)
 {
   Debug.Log("vector4");
 }
 
 void Start()
 {
   Test(Color.red);
 }

This is apparently due to the fact that Vector4 and Color can be implicitly cast to each other (and are part of the UnityEngine DLLs), and the compiler, in case there are added optional parameters, goes crazy. The issue is purely Unity-related (a Unity Assembly generated in Visual Studio doesn't generate any ambiguity, even if it contains calls to methods with implicitly castable types and optional parameters).

In short, the overloads and relative call in EXAMPLE B should totally work, while instead they don't.

Can anyone share some light on this? Or it's just a known bug?

EDIT

Just out of curiosity, tswalk notes that calling Test and including the optional parameter like this:

 Test(Color.red, true);

works both with EXAMPLE A and B

Comment
Add comment · Show 9
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 iamthecoolguy11 · Jul 10, 2014 at 04:22 PM 0
Share

what does this script do and what is Test(col); calling

avatar image Demigiant · Jul 10, 2014 at 04:25 PM 0
Share

The script doesn't do anything: it's just for testing purposes in order to replicate the issue. Copy/pasting it inside a $$anonymous$$onoBehaviour will generate the ambiguity error. "col" is just a Color, but I now replaced it with Color.red so it's more clear

avatar image iamthecoolguy11 · Jul 10, 2014 at 04:33 PM 0
Share

so your having copy and pasting problems with monoBehaviour? $$anonymous$$y friend has problems with that and if thats what the problem is you have to reinstall windows usually. but theres a lot to try befor that.

avatar image Demigiant · Jul 10, 2014 at 04:42 PM 0
Share

Uh no, it's not a copy/paste error :P I meant that you can copy/paste that same code and see that it generates an error, while ins$$anonymous$$d it should not (because the overloads in EXA$$anonymous$$PLE B should totally work).

avatar image tswalk · Jul 10, 2014 at 05:59 PM 1
Share

had to do some research on the optional parameters and it seems C# 4.0 has spoiled us a bit there... since Unity/$$anonymous$$ono is stuck at 2.0 compatibility with its' own apparent variant on 3.0/.5, seems that optional parameters don't work.

[edit] also, using your example.. if you force the "optional" parameter in calling, the ambiguity goes away... and the overloaded method signature is recognized properly.

Show more comments

0 Replies

· Add your reply
  • Sort: 

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

24 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

Related Questions

Vector4 and Color messing up with method overloading 1 Answer

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Overloaded methods with one minor difference... Any way to refactor? 2 Answers

Renderer on object disabled after level reload 1 Answer


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