• 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
7
Question by Michael La Voie · Nov 25, 2009 at 06:52 AM · monocompiling

The type or namespace 'var' could not be found (in C#)

Does unity support the C# keyword var and if not are there plans to support it?

EDIT

I did some research and found that mono seems to support three versions of C#: 1, 2 and 3. In 2007, they added support to C# 3 for the var keyword. I'm not sure what version of mono is being used in Unity and if it should support var (maybe I'm doing something wrong).

Thanks!

Solution

As @The Stone and @Ricardo were nice enough to point out, Unity is currently supporting an older version of mono that doesn't yet support the C# 3 feature set (var, LINQ, etc). They are investigating it and would have implemented it with Unity 2.6, but it would break backwards compatibility.

Currently there is a lot of interesting discussion in the user voice page for it so feel free to read about it/vote it up if it is important to you.

Update

Hey all, with the Unity 3.0 announcement and a pending release in Summer 2010 it looks like this feature will be implemented. I can't wait.

Comment
Add comment · Show 1
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 Ashkan_gc · Mar 05, 2010 at 08:29 AM 0
Share

the unity iphone 1.6 is upgraded to mono 2.6 so they will upgrade the unity too. they might do this in next $$anonymous$$or release.

7 Replies

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

Answer by TowerOfBricks · Nov 25, 2009 at 07:39 AM

Unity does not use the latest version of mono. The latest version is 2.4 and 2.6 will be out soon, right now Unity uses an 1x version, they haven't upgraded due to doing so will break all existing web player games. But I guess they will do so when Unity 3.0 comes, I hope so at least.

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 Michael La Voie · Nov 25, 2009 at 04:45 PM 0
Share

Thanks for the clarification. Its not the end of the world, but I'm with you. I really hope they update with version 3.0. I think I'll head over to user voice...

avatar image Ricardo · Nov 25, 2009 at 05:12 PM 0
Share

Aras mentioned on the feedback page they wanted to do it for 2.6, but had to delay it. There's no estimated deadline for it yet: http://feedback.unity3d.com/pages/15792-unity/suggestions/158233-scripting-upgrade-mono-to-2-4-2-6?ref=title

avatar image
11

Answer by Lance Sun · Dec 09, 2009 at 01:52 AM

Here's a trick I am using to get C# 3.0 language support for var, extension methods, lambda expressions, object initializers, collection initializers, implicitly typed arrays, expression trees, auto implemented properties, and query expressions.

I've only done this for Windows. The Mac instructions are probably analogous.

Download and install the latest version of Mono (2.6.1)

Copy these files: gmcs.exe, gmcs.exe.config

From: C:\Program Files\Mono-2.6.1\lib\mono\2.0

To: \Unity\Editor\Data\MonoCompiler.framework

This only works because C# 3.0 compiles into IL compatible with the 2.0 runtime. Though, linq to objects does require a change to System.Core (which came with .NET Framework 2.0 SP1)... but linq code compiles and runs fine for me, so I guess the version of mono that Unity is using includes this change. What definitely doesn't work is linq to sql, because that requires System.Data.Linq

Comment
Add comment · Show 7 · 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 Michael La Voie · Dec 09, 2009 at 06:37 AM 0
Share

Holy moly! Thats brilliant, I'm sure it's not "officially" supported, but I'm going to try that out this weekend. If this works, you will have saved my butt. I'll come back and update once I've tested it. Thank you!

avatar image Ricardo · Dec 09, 2009 at 01:08 PM 0
Share

If I could downvote myself to vote you up more than once, I would. I can confirm this as working on OSX, even if the files you'll need to copy are slightly differently named. There's no mono.dll, and the libglib and libgmodule will have .dylib extensions. This will get you var declarations, collection initializers... the works.

avatar image Froghut · Mar 04, 2010 at 12:08 PM 0
Share

To get this to work with $$anonymous$$ono 2.6 you must copy the file libgthread-2.0-0.dll from the mono bin folder, too. Thanks for this "hack"!!

avatar image Lance Sun · Mar 05, 2010 at 12:53 AM 0
Share

whoops, you're right. and you don't need libgmodule-2.0.0.dll

avatar image Lance Sun · Mar 05, 2010 at 03:00 AM 0
Share

Update: If you are using LINQ in your project, it will work. If you are using a prebuilt assembly targeting .NET 3.5, and that assembly uses LINQ, you will get an error message in Unity about System.Func`2`. You can copy C:\Program Files\$$anonymous$$ono-2.6.1\lib\mono\2.0\System.Core.dll to Unity\Editor\Data\$$anonymous$$onoCompiler.framework and Unity\Editor\Data\Frameworks\$$anonymous$$ono.framework to get it to work. But keep in $$anonymous$$d that since you are modifying the runtime framework, it won't work with the WebPlayer.

Show more comments
avatar image
2

Answer by LoganBarnett · Apr 10, 2010 at 01:18 PM

I posted an upgrade script here: http://github.com/LoganBarnett/unity-mono-upgrade It's for OS X only. Feel free to submit a Windows version and I'll post it there.

Comment
Add comment · Show 1 · 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 Michael La Voie · Apr 10, 2010 at 06:24 PM 0
Share

Very cool! Thanks for posting it

avatar image
1

Answer by Ricardo · Nov 25, 2009 at 02:29 PM

Your guess is correct TheLameDuck - Unity is still only up to C# 2.0. That means no var declaration, no nice collection initializers, and no Linq.

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
avatar image
-2

Answer by Jaap Kreijkamp · Nov 25, 2009 at 06:55 AM

No, in C# you define the variable by type so:

int x;

instead of:

var x;

and

GameObject go;

instead of:

var go : GameObject;
Comment
Add comment · Show 3 · 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 Michael La Voie · Nov 25, 2009 at 07:02 AM 0
Share

Thanks, but beginning with C# .net 3.0 $$anonymous$$S introduced the var keyword: http://msdn.microsoft.com/en-us/library/bb383973.aspx. It isn't like the JS equivalent in that it can't be set to multiple types, it is just a shorthand.

avatar image Michael La Voie · Nov 25, 2009 at 07:03 AM 1
Share

$$anonymous$$y guess is that the version of mono that is supported doesn't yet have the var keyword, but I was hoping to get confirmation. Its not a huge deal, but within the C# community its starting to become "good practice" to use var when initializing.

avatar image Ashkan_gc · Dec 16, 2009 at 08:28 AM 0
Share

C# compiler will choose the type from righthand side data when see the var keyword in lefside of identifier name. i hope that and think that unity 3 will support latest version of mono's C# compiler that is C# 4 in that time. hopefully we have dynamic language features in C# for web services and wow optional parameters :D :D :D

  • 1
  • 2
  • ›

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

No one has followed this question yet.

Related Questions

Dynamically loading scripts 4 Answers

AOT Cross Compiler - Fails without Details! 5 Answers

Does Unity always use Ahead-Of-Time compilation? 1 Answer

Can I control when unity compiles scripts? 1 Answer

Is Unity Engine written in Mono/C#? or C++ 4 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