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 /
avatar image
0
Question by DaiMangouDev · Oct 03, 2015 at 11:37 PM · c#visual studiodll.net

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.

alt text

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

alt text

ne1.jpg (16.1 kB)
ne15.jpg (15.4 kB)
Comment
Add comment · Show 9
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 RLin · Oct 04, 2015 at 01:10 AM 0
Share

It might be because vector2 requires that you assign the actual values for the vector2 like this:

new Vector2 (x,y);

avatar image DaiMangouDev RLin · Oct 04, 2015 at 01:22 AM 0
Share

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

avatar image RLin · Oct 04, 2015 at 01:25 AM 0
Share

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).

avatar image DaiMangouDev RLin · Oct 04, 2015 at 02:53 AM 0
Share

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

avatar image Suddoha · Oct 04, 2015 at 01:31 AM 0
Share

$$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.

avatar image DaiMangouDev Suddoha · Oct 04, 2015 at 02:44 AM 0
Share

doesn't work either. I have no idea what is wrong. I want to fix the problem

avatar image Suddoha DaiMangouDev · Oct 04, 2015 at 12:06 PM 0
Share

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.

avatar image Dave-Carlile Suddoha · Oct 04, 2015 at 03:30 AM 0
Share

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?

avatar image DaiMangouDev Dave-Carlile · Oct 04, 2015 at 03:49 AM 0
Share

Setting it to Vector2.zero wont work either. I am using .NET Framework 3.5 as always

1 Reply

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

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);
 }
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 DaiMangouDev · Oct 04, 2015 at 11:21 AM 0
Share

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 ?

avatar image Bunny83 DaiMangouDev · Oct 05, 2015 at 12:59 AM 0
Share

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.

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

32 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

Related Questions

OIDC Authentication DLL compatability and web browser help 0 Answers

Are switch -expressions- compatible with Unity? 1 Answer

System.Net.WebSockets Namespace not available in Unity generated Project 2 Answers

Multiple Cars not working 1 Answer

How to link a class Library project in visual studio with my generated visual studio .sln unity project ? 1 Answer


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