- Home /
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?
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.
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!
@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.
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.
The issue has been fixed on Windows, however the bug is still present on $$anonymous$$ac.
This is super annoying still and as a beginning really getting in the way of my progress in learning
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....
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
(C# and Java)Unable to give the Damage from enemy bullet script to Player Health. 3 Answers
Compiler error when using ArraySegment 0 Answers
"Only assignment, call, increment, decrement and new object expressions can be used as statements" 1 Answer