- Home /
Why are Vector2 parameters of methods , set to be = new Vector2 , read as being null by dll ?
I started getting this error as soon as i make my code into a dll.
Argument #' cannot convert null' expression to type UnityEngine.Vector2'
I did not tget this error when my code was just a c# script.
Okay here is a before and after synopsis.
Initially I had a regular c# script named ScriptA in my unity project .
This is ScriptA
namespace MyScript
{
public class ScriptA
{
public void MethodA(Rect rect, int i, Vector2 position = new Vector2())
{
}
}
}
I then use ScriptA.MethodA , and as you see , the Vector2 parameter is marked as = new Vector2()
.awesome.
I then moved ScriptA to a Class Library and made a dll out of it . Place the dll into my unity project. it wors , the clases are accessed, However for some reason as you see in this picture the Vector2 parameter in MethodA sa marked as = null
even though in the dll it is set to = new Vector2
It might be because vector2 requires that you assign the actual values for the vector2 like this:
new Vector2 (x,y);
That will not work, this error will be thrown, Default parameter value for 'position' must be compile-time constant
by default new Vector2() is Vector2.zero
That doesn't seem right at all. This has never happened to me when I assigned the values. (Just to be sure, make sure you use actual numbers and not x and y for the values).
I just built out a different dll that uses the same method and I still have the same problems . This feels strange indeed . I never expected this. why would it ever say null
$$anonymous$$aybe you could replace
new Vector2()
with
default(Vector2)
as new Vector2 is the default anyway.
If it doesn't work either, I'd suggest to use another overload as long as you cannot find a solution.
doesn't work either. I have no idea what is wrong. I want to fix the problem
It works both ways for me, that's really strange :S But I tend to use different overloads anyways. That's also less confusing because you see the different ways of using the method just right from the beginning ins$$anonymous$$d of having an optional parameter. That can be really confusing with long parameter lists if you only need a few of them.
Same with constructors. Implement the logic once and let other constructors call the one which is implemented.
How about getting rid of the default altogether? Just pass in Vector2.zero if you don't have another value for it. It seems like I remember seeing early versions of Unity/$$anonymous$$ono having trouble with default parameters so maybe there's something there.
Also, what version of .NET are you compiling your dll to?
Setting it to Vector2.zero wont work either. I am using .NET Framework 3.5 as always
Answer by Bunny83 · Oct 04, 2015 at 04:32 AM
Default parameters always were a bit problematic in Unity. However I've never seen this behaviour. It's probably the best to do what Unity does with most of it's own defaul parameter methods: Just declare an overload
public void MethodA(Rect rect, int i, Vector2 position)
{
// your actual method
}
// overload without the Vector2 parameter
public void MethodA(Rect rect, int i)
{
MethodA(rect, i, Vector3.zero);
}
Overloads were my last resort because, but this i great because I don't need to duplicate my entire method. Thanks a a lot.
Are you able to try the example code for yourself ?
Not at the moment, but i might try it the other day^^. In the past default parameters weren't supported at all in Unity. So i always implemented an overload ins$$anonymous$$d. Of course default parameters are a nice feature, but i prefer to keep my code backwardscompatible as much as possible.