7

I'm trying to use PriorityQueue in Unity with C#. The documentation says that it's supported in .NET 6 in namespace System.Collections.Generic.

I've tried:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using TMPro;

public class Test : Monobehaviour
{
    void Start()
    {
        var queue = new PriorityQueue<int, int>();
    }
}

But an error is thrown back:

The type or namespace name 'PriorityQueue<,>' could not be found (are
you missing a using directive or an assembly reference?)
[Assembly-CSharp]

I've checked the .NET version within VS Code: enter image description here

Why doesn't it work in Unity?

7
  • 2
    What is the target framework version in the project file? Just running dotnet from a prompt doesn't prove much. Jan 3, 2022 at 15:50
  • @JeroenMostert I found this in the .csproj file <TargetFrameworkVersion>v4.7.1</TargetFrameworkVersion>. This is the problem?
    – silverfox
    Jan 3, 2022 at 15:55
  • 2
    Well yes, it's obviously targeting .NET 4.7.1, and given the name of that element this is an old-style .csproj, not the new-style (that uses TargetFramework and TargetFrameworks, and is a lot shorter besides). I'm not sure Unity even has support for .NET Core (yet), which 6 is, but that's another question. Jan 3, 2022 at 15:58
  • 2
    Fortunately priority queues are not rocket science (if that's the only thing you're going for in terms of .NET 6); compatible implementations should be easy to find. Sufficiently unimportant queues that aren't crucially dependent on performance can even be faked with other data structures (like a SortedList or SortedDictionary using a tuple that includes the priority and an ID for uniqueness, with only the priority used for sorting through a custom comparer). Jan 3, 2022 at 16:16
  • 1
    I have the same issue. I checked visual studio installer and know .NET 6 is installed. Project target framework is .NET Framework 4.8. I'm also just using this in a console application in visual studio, nothing special like unity involved. Feb 1, 2022 at 21:07

1 Answer 1

21

The version reported by dotnet is completely unrelated to Unity's C#/.NET version (at least until Unity finally migrates from Mono). While the language supported is almost equal to C# 9.0, the actual library that is used is a loose superset of what Mono provides, and actually is roughly equal to .NET Framework 4.8/.NET Standard 2.0 ATM; since neither of those has PriorityQueue, it's not available. You can either:

1
  • 1
    Yeah, I have imported C5 and used their IntervalHeap since then, pretty fine!
    – silverfox
    Aug 21, 2022 at 1:45

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.