Home > Software engineering >  error CS0246: The type or namespace name 'CsvHelper' could not be found
error CS0246: The type or namespace name 'CsvHelper' could not be found

Time:09-12

I'm new to Unity and C#, hopefully my question is clear enough.
It's a Unity test project to show the issue only.
On Mac: I created a new class and opened it with Visual Studio, added the CsvHelper package using: Tools => Manage NuGet Packages => Add Package.
The package was successfully added to packages as shown here Solution

Once that was done, I was able to add the code line: using CsvHelper; with Visual Studio intellisense recognizing it.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using CsvHelper;

public class Level {

}

Moving back to Unity, it showed me the error:
error CS0246: The type or namespace name 'CsvHelper' could not be found (are you missing a using directive or an assembly reference?)
Switching back to Visual Studio, it started to show an error.

On Windows: I have done the same, had the same error, except I also tried adding reference through: Project => Add Reference. Once that is done, the error disappears from Visual studio, still exists in Unity.
closing Visual Studio will recreate the error there, and the reference that I added disappears from the Solution explorer under References.

I have no clue of what I'm missing, tried almost every suggestion on here that I was able to understand. excuse my lack of understanding of the terminology and the proper practices.

CodePudding user response:

There are two points you need to understand to solve your problem properly.

  1. You have a *.csproj file, where all your dependencies like CsvHelper, UnityEngine and so on specified. When Visual Studio inspect a code, it bases on this *.csproj file to show errors like "Missing namespace".
  2. This *.csproj file means nothing for Unity. Unity generates these files automatically to make your IDE (Visual Studio, Rider or any else) work properly.

So, it means, when you add a dependency in Visual Studio it means only Visual Studio knows about this dependency, so it shows no error. But when Unity checking this code, it doesn't see this dependency, so it shows an error.

To add a dependency in Unity, generally, you can just place a *dll file somewhere in Assets/ folder. When you do this, Unity will automatically regenerate *.csproj files and add this dll as a dependency for them. You can read more about it here https://docs.unity3d.com/Manual/UsingDLL.html.

You mentioned NuGet, so I think this plugin for Unity will be helpful for you. You can easily manage nuget staff in your Unity project with this. https://github.com/GlitchEnzo/NuGetForUnity

Probably you don't use Unity AssemblyDefenition files. But if you do, so, after adding dll to Asset/ folder, you also have to specify it in this *.asmdef file. You can read more about it here https://docs.unity3d.com/Manual/ScriptCompilationAssemblyDefinitionFiles.html.

  • Related