Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by Don-Fear · Jun 17, 2016 at 11:22 AM · functionparametersoverload

Function overloading with and optional arguments

Hi folk. I need your help. Can you explain script behaviour?

There is a test project with one button and one script.

script:

 using UnityEngine;
 using System;
 
 public class TestScript : MonoBehaviour {
   private void TestFunc(object value, Action action = null) {
     Debug.Log("Single");
   }
 
   private void TestFunc(object[] value, Action action = null) {
     Debug.Log("Array");
   }
 
   public void OnClick() {
     object single = new object();
     this.TestFunc(single); // <-- All good
 
     object[] array = new object[] { };
     this.TestFunc(array); // <-- Error is here
   }
 }

MS VS 2016 doesn't show any errors.

But I received error in Unity:

Assets/TestScript.cs(18,10): error CS0121: The call is ambiguous between the following methods or properties: 'TestScript.TestFunc(object, System.Action)' and 'TestScript.TestFunc(object[], System.Action)'

PS: sorry for my English

PPS: this is compiler bug

I'm glad to inform that we have fixed this and will be released with the new version of our compiler (on Unity 5.5)

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

1 Reply

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

Answer by btmedia14 · Jun 17, 2016 at 05:41 PM

The first function with the single Object signature

 private void TestFunc(object value, Action action = null)

allows the function to to be called with any object as the first parameter. Object is the base class for everything in Unity. As such it will accept arrays of objects or any other datatypes derived from Object. An array of Objects is just another Object. However, in the case of Object[] as a parameter, the compiler also happens to pattern match against the second method signature. Hence the ambiguity. If the datatypes were other than Object, say int, the method signatures would be considered unique enough when being called with an array of ints.

Comment
Add comment · Show 5 · 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 Don-Fear · Jun 20, 2016 at 06:27 AM 0
Share

But why in case of

 private void TestFunc(object value)

and

 private void TestFunc(object[] value)

There are no errors?

avatar image btmedia14 Don-Fear · Jun 21, 2016 at 02:15 AM 0
Share

That is interesting. There should be compile time errors in both sets of signatures. I checked under Visual Studio 2015 as well as $$anonymous$$onoDevelop and do see compiler errors of the type you listed earlier in both cases. For Visual Studio ensure the error filter within the Error List window is not suppressed else no errors will be listed even though errors are generated.

avatar image Don-Fear btmedia14 · Jun 21, 2016 at 06:36 AM 0
Share

Here is screen with both situations. And i have only one error message only in unity. Both situations. New Code alt text Interesting! If I reload Unity, without change any code in VS, I have NO errors in Unity and I can run project. But executed only "single" version of "TestFunc_1" After Unity reloading If I change any code in VS, i receive same Unity error.

And here is VS-only console project. VS-only console project. Everything is O$$anonymous$$!

unity3.png (90.4 kB)
unity.png (226.8 kB)
Show more comments

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

59 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 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Is it possible to overload methods from unity engine ? 0 Answers

Is this bug known? function parameter s and if statements c# 1 Answer

Component as parameter 1 Answer

Failed to call fundtion... on this script which makes no sense 0 Answers

Put array in function 'parameters ? 2 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