Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Script can use namespace or optional parameters, but not both

Discussion in 'Editor & General Support' started by Cygon4, Jan 2, 2013.

  1. Cygon4

    Cygon4

    Joined:
    Sep 17, 2012
    Posts:
    382
    I've used optional parameters some of my methods, eg.

    Code (csharp):
    1. class Mooh : MonoBehavior {
    2.   void Update() {
    3.     doSomething();
    4.   }
    5.   private void doSomething(int times = 1) {
    6.     // ...
    7.   }
    8. }
    This has never posed a problem until I noticed that contrary to what (http://docs.unity3d.com/Documentation/ScriptReference/index.Writing_Scripts_in_Csharp_26_Boo.html) says, namespaces for MonoBehavior-derived scripts seem to be supported by Unity 4.0 and even produce tidy nested menu levels in the component selector:



    Now what confuses me is that I cannot use both at the same time. I mean, if a MonoBehavior script has a method with an optional parameter, no problem. If a MonoBehavior script is in a namespace, no problem as well. But if a MonBehavior script is in a namespace AND has a method with an optional parameter, it breaks (the console message falsely claims the name of the class wouldn't match the file).
     
  2. smb02dunnal

    smb02dunnal

    Joined:
    May 5, 2012
    Posts:
    439
    I to am experiencing this problem, and I'm finding it quite frustrating!!! Thought I was alone until I came across this thread.

    Any idea about a fix Unity?
     
  3. jedy

    jedy

    Joined:
    Aug 1, 2010
    Posts:
    579
    Heya,

    I've encountered the same thing.

    To get it working - do not use optional parameters in the Update method. They screw up big time. Encapsulate them in a class and it should work fine.
     
  4. Shaun-Peoples

    Shaun-Peoples

    Joined:
    Feb 26, 2013
    Posts:
    24
    Not true, any method that uses default arguments in the method signature, even those not overridden/etc from MonoBehaviour will still cause issues and produce either error, depending on how the script is attached:

    - If the script is already attached and you change the method signature, or add through add components
    "The associated script can not be loaded. Please fix any compile errors and assign a valid script"

    - If the script is dragged onto the object to add
    Name script mismatch

    This should either be a documented problem to avoid or a fix, because I spent two hours on this.
     
  5. Arges

    Arges

    Joined:
    Oct 5, 2008
    Posts:
    359
    It appears this is still an issue. It is not limited to the Update method, but any method signature.
     
  6. jedy

    jedy

    Joined:
    Aug 1, 2010
    Posts:
    579
    Confirmed.
     
  7. sebas77

    sebas77

    Joined:
    Nov 4, 2011
    Posts:
    1,641
    I tell you what...you should not REALLY use optional parameters in c#. They have been introduced only to be compatible with CLI languages and they do not belong to the c# syntax.
     
  8. BonyYousuf

    BonyYousuf

    Joined:
    Aug 22, 2013
    Posts:
    110
    This is killing me. Any idea when we will get a fix?
     
  9. Demigiant

    Demigiant

    Joined:
    Jan 27, 2011
    Posts:
    3,239
    I find this bug quite terrible. Bumping the thread.

    @sebas77: (hey Seba ;)) whatever the reason for introducing optional parameters, they are indeed very useful, especially to keep the code tidy when you don't really need overloads. Is there some real reason why we should not use them (Unity bugs apart)?
     
  10. jedy

    jedy

    Joined:
    Aug 1, 2010
    Posts:
    579
    Optional parameters are simply syntactic sugar. There are some minor pitfalls but with sensible code there are only benefits using them.
     
  11. codestage

    codestage

    Joined:
    Jul 27, 2012
    Posts:
    1,931
    I encounter this bug often too, wish it to be fixed. I submitted a bug report about it in september (remark - I submitted comment to that bug report):

    http://issuetracker.unity3d.com/iss...ters-in-its-method-signature-at-the-same-time

    It was marked as duplicate of this:
    http://issuetracker.unity3d.com/issues/combining-namespaces-mbs-with-default-params-break

    It's marked as fixed. But it can be easily reproduced in 4.3.3!

    So we have this weird bug since 2012 (at least) and looks like no one going to fix it since they think it is fixed.
     
    Last edited: Feb 3, 2014
  12. jedy

    jedy

    Joined:
    Aug 1, 2010
    Posts:
    579
    I remember voting on this a long time ago.

    Anyway I posted a reply hoping someone would see it. I don't see a way to post new things.
     
  13. Rob-A

    Rob-A

    Joined:
    Aug 7, 2012
    Posts:
    33
    This bug was fixed for classes only. But they did not fixe it for structs containing methods with default arguments or static functions as well. This gives me a lot of headache the first time i encountered it. Unity pls fix this bug with all its corner cases.
     
  14. Demigiant

    Demigiant

    Joined:
    Jan 27, 2011
    Posts:
    3,239
    The Unity guys told me it's marked as fixed for the next release. Crossing fingers :)
     
  15. cjke-7777

    cjke-7777

    Joined:
    Apr 28, 2013
    Posts:
    9
    God I just spent two hours slowly by slowly pulling out various bits of code trying to figure out what was actually triggering this problem. To the point where I had it narrowed down to the presence of one function. Then I removed each argument one by one, damn frustrating.

    At least there are others in the same boat! :)
     
  16. Zinov

    Zinov

    Joined:
    Jul 20, 2015
    Posts:
    38
    Thats just terrible
     
  17. rad1c

    rad1c

    Joined:
    Feb 26, 2016
    Posts:
    21
    "same same but different, yet still the same" - I add an optional parameter (with default value) to a public function and want to attach the function to an EventTrigger (mouseover for example) - not possible. The function doesn't even appear on the list, acts like if it was a private member...
     
  18. jedy

    jedy

    Joined:
    Aug 1, 2010
    Posts:
    579
    AFAIK - the surefire way to go around that is to overload the method.
    Playing around with namespaces also works sometimes.
     
  19. felixmann

    felixmann

    Joined:
    Feb 16, 2017
    Posts:
    11
    Just ran into this with Unity 5.5.1f1 :/