2

working with unity and c# and when I try these line

using System.Xml.Linq;

I get this error

The type or namespace name Linq' does not exist in the namespace System.Xml'. Are you missing an assembly reference?

So I found online, through a lot of searching, that Linq can cause problems, and you need to manually add a reference in MonoDevelop. So I added a reference there (by right clicking References in the solutions pane, clicking Edit References and browsing for System.Xml.Linq)

Still no luck,

Any ideas?

Steven
  • 166,672
  • 24
  • 332
  • 435
  • 1
    possible duplicate of [How do I use Linq in MonoDevelop 2.0 on OS X?](http://stackoverflow.com/questions/871230/how-do-i-use-linq-in-monodevelop-2-0-on-os-x) – Steven Jul 24 '13 at 17:25
  • Maybe it is, but after following the answers in that question, Unity (not MonoDevelop) still gives me errors (see comment in the answer below) –  Jul 25 '13 at 08:01

3 Answers3

2

I think this SO post will answer your question...

https://stackoverflow.com/a/875704/1246574

It's pretty much the same issue you're having.

Community
  • 1
  • 1
Jim
  • 6,753
  • 12
  • 44
  • 72
  • Thanks, that's exactly what I needed! Obviously missed that one –  Jul 25 '13 at 07:24
  • Actually, I'm removing the check for now. Although the answer helped me compile in MonoDevelop, Unity still detects the error. There must be some other setting I need to change. I both checked my Mono / .NET version (it was already 3.5) and added the System.Xml.Linq reference in my solutions pane –  Jul 25 '13 at 08:00
0

You can't modify the monodevelop (or visual studio) solution or project manually. Because they are generated by Unity. Even if you modify it, Unity can overwrite your changes when it regenerates the solution files.

The reason you can't use some namespaces is the .net/mono version selected in your Unity project settings.

Search for "API Compatibility Level" in this documantation http://docs.unity3d.com/Documentation/Manual/class-PlayerSettings.html

C'mon people, Stop modifying the generated code files. This is 2013!

Xtro
  • 301
  • 2
  • 12
  • Thanks for the info, can't believe I never saw this online anywhere. This would seem like an important issue. Anyway, I'll check it out when I get back to work after the weekend –  Jul 26 '13 at 10:22
  • Hope it helps. Don't forget to mark the answer for community's sake if it works. Btw, I prefer not to use Linq in my Unity development. There is something telling me not to do so. – Xtro Jul 26 '13 at 13:19
  • still no luck, see my answer for the solution...now I understand why you try to avoid Linq. What's the alternative? –  Jul 28 '13 at 06:13
  • Unity supports .NET 3.5 and Linq. The reason you don't see 3.5 in project settings is that All .NET versions are built on version 2.0. If you select the full .net mode (other than "subset"), you should be able to use Linq in your code. But still, I don't recommend. – Xtro Jul 29 '13 at 14:55
  • API Compatibility Level = .Net 2.0 (not subset) is the real answer to that question. You should mark this. – Xtro Jul 29 '13 at 14:59
  • Uh, sorry. Like I said, I tried that and it didn't work. Maybe it's a glitch in Unity, but until I dragged the .dll into Unity, it was giving errors –  Jul 30 '13 at 06:02
  • I don't prefer to use Linq in unity but I tested it and it works actually. So Unity DOES support .NET 3.5 (Maybe not fully but mostly). Marking your own answer(which is wrong) is not good. – Xtro Jul 30 '13 at 13:37
  • Ok, I'm just telling you what I'm seeing. I currently have my API Compatibility Level set to .NET 2.0 Subset and it's working. When I delete the System.Xml.Linq.dll file from Unity, it stops working. I believe you that was your solution. It's possible there are other factors that affect it. I'll remove the check and edit my answer for clarification. Thanks for the feedback –  Jul 31 '13 at 05:47
  • Ohhh wait wait wait!! System.Xml.Linq is in .NET4.5 :) :) You should use System.Linq namespace for simple linq queries. Do you realy need xml version of it ??? – Xtro Jul 31 '13 at 13:31
0

from this post, I found that Unity doesn't support .NET 3.5 by default (which is required to use Linq). In the settings Xtro talked about, you can only choose .NET 2.0. Instead, you need to drag the desired .dll file into Unity like a texture. Kinda hackish, but it works. Once again, like Xtro said, there is NO REASON to edit the MonoDevelop settings. They have no affect on the final compilation.

EDIT: See Xtro's answer for an alternate solution. He was able to make Linq work by changing the API Compatibility Level to .NET 2.0 (not Subset).

EDIT 2: I just updated from Unity 4.1 to 4.2 today and got an error that Linq was defined twice. by deleting the .dll, I solved the error. Note I'm still using .NET 2.0 Subset. Looks like they added Linq support by default.

Community
  • 1
  • 1