• 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 /
This post has been wikified, any user with enough reputation can edit it.
avatar image
1
Question by donurks · Feb 17, 2014 at 03:21 PM · c#script error

Why can't assign script to object when class is in namespace and method uses default values

Why i can't assign a script (c#) containing a namespace and a method with default values to a gameobject?

 // error: Can't add script behaviour Test. The scripts file name does not match the name of the class defined in the script!
 using UnityEngine;
 
 namespace Urks
 {
     public class Test : MonoBehaviour
     {
         public void TestMethod(int param = 5) {}
     }
 }
 
 // works
 using UnityEngine;
 
 namespace Urks
 {
     public class Test : MonoBehaviour
     {
         public void TestMethod(int param) {}
     }
 }
 
 // works
 using UnityEngine;
 
 public class Test : MonoBehaviour
 {
     public void TestMethod(int param = 5) {}
 }


Is there any hint? Can anybody reproduce this error?

Comment
Add comment · Show 3
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 DoctorSauce · Feb 17, 2014 at 03:35 PM 0
Share

It's saying the name of the script in Unity doesn't match the name that the script is giving itself. On your first script, I think you might need to take off the "= 5" and make the first thing in the function be

 param = 5;

I think what is really messing with Unity is the namespace Urks, I've never seen a namespace and I'm not sure what that means, so if it's possible to just take the namespace out and leave it like the third script, that would probably cause less problems in the future. But if not, try what I said above.

avatar image donurks · Feb 17, 2014 at 03:42 PM 0
Share

I've never seen a namespace Namespaces are a common way to organize code elements. Even UnityEngine is a namespace. They work fine so far - just the class with the default param doesn't work unexplainable. I don't look for a workaround - i just want to understand why :). Thanks anyway!

avatar image Rafes · May 03, 2014 at 05:46 PM 0
Share

@DoctorSauce: If you've never seen a namespace before it is probably best not to offer solutions about a namespace issue. Namespaces work. This is indeed a strange issue in the OP. Looks like a bug. See the accepted answer.

2 Replies

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

Answer by RudyTheDev · Feb 18, 2014 at 01:11 PM

This is apparently a known bug that has been reported and according to this thread it will be fixed in next patch.

Comment
Add comment · Show 2 · 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 ilya_ca · Sep 29, 2014 at 02:31 AM 0
Share

The issue has been fixed on Windows, however the bug is still present on $$anonymous$$ac.

avatar image bbyford · Jan 15, 2015 at 04:22 PM 0
Share

This is super annoying still and as a beginning really getting in the way of my progress in learning

avatar image
0

Answer by Rafes · May 03, 2014 at 10:18 PM

I just had this happen in 4.3.3f1. It was caused by having just the getter of a property inside a preprocessor block. If I removed the namespace it worked fine. If I moved the entire property in to the preprocessor it also worked fine; which is what I ended up doing.

I guess we should treat this error as a general clue to go looking for things like the use of preprocessor directives, attributes and other special use case items (for lack of a better description of these kinds of patterns). ...and of course by "go looking" I mean commenting stuff out until it works, and then bringing stuff back until it does not....

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

21 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

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

im having a problem lerping text color between 2 color 1 Answer

An object reference is required for non-static field, method, or property 1 Answer

(Problem) Turning postprocessing on and off 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