x


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.

more ▼

asked Nov 25 '09 at 06:52 AM

Michael La Voie gravatar image

Michael La Voie
2.3k 7 12 43

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

Mar 05 '10 at 08:29 AM Ashkan_gc
(comments are locked)
10|3000 characters needed characters left

7 answers: sort voted first

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.

more ▼

answered Nov 25 '09 at 07:39 AM

TowerOfBricks gravatar image

TowerOfBricks
3.2k 17 25 50

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

Nov 25 '09 at 04:45 PM Michael La Voie

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

Nov 25 '09 at 05:12 PM Ricardo
(comments are locked)
10|3000 characters needed characters left

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

more ▼

answered Dec 09 '09 at 01:52 AM

Lance Sun gravatar image

Lance Sun
1k 12 16 41

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!

Dec 09 '09 at 06:37 AM Michael La Voie

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.

Dec 09 '09 at 01:08 PM Ricardo

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

Mar 04 '10 at 12:08 PM Froghut

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

Mar 05 '10 at 12:53 AM Lance Sun

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.Func2. You can copy C:Program FilesMono-2.6.1libmono2.0System.Core.dll to UnityEditorDataMonoCompiler.framework and UnityEditorDataFrameworksMono.framework to get it to work. But keep in mind that since you are modifying the runtime framework, it won't work with the WebPlayer.

Mar 05 '10 at 03:00 AM Lance Sun
(comments are locked)
10|3000 characters needed characters left

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.

more ▼

answered Apr 10 '10 at 01:18 PM

LoganBarnett gravatar image

LoganBarnett
36 1 4

Very cool! Thanks for posting it

Apr 10 '10 at 06:24 PM Michael La Voie
(comments are locked)
10|3000 characters needed characters left

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.

more ▼

answered Nov 25 '09 at 02:29 PM

Ricardo gravatar image

Ricardo
5.2k 20 32 96

(comments are locked)
10|3000 characters needed characters left

We managed to replace only the gmcs.exe (without replacing any other file) And almost all 3.0 languages constructs became supported.

Since it's a compile time only change it seens pretty safe. We tested this change with a project with hundreds of c# classes

We used unity 2.6.1f1 and gmcs.exe 2.0.50727.143

more ▼

answered Jan 13 '10 at 11:47 AM

Daniel Sperry 2 gravatar image

Daniel Sperry 2
662 11 15 30

you're right... you don't need a new mono.dll (or the related files). i've updated the instructions

Mar 07 '10 at 02:05 PM Lance Sun
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

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

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x181
x72

asked: Nov 25 '09 at 06:52 AM

Seen: 9283 times

Last Updated: Mar 10 '10 at 06:50 PM