Error - The name 'UnityServices' does not exist in the current context

I’m looking at setting up the Unity Analytics service in a project. I’ve linked the project to the service and have included the code snippet for the Analytics service in the manifest.json file:
“com.unity.services.analytics”: “3.0.0-pre.4”

I’ve added the “using Unity.Services.Analytics;” at the top of my class, and have also included the below code:

// Start is called before the first frame update
    async void Start()
    {
        await UnityServices.InitializeAsync();
    }

However, I seem to be getting the following error:

error CS0103: The name ‘UnityServices’ does not exist in the current context

Would anyone know why this error is occurring?

had the same problem 5 minutes ago, documentation is quite complicated to follow as it keeps promoting that gaming services beta plugin in every page, and some guides seem to indicate they are for the new and some for the old analytics?

i went back to legacy analytics plugin from package manager for now… but interested to know whats the solution for that beta…

Hi Kilfeather and mgear,
Thanks for posting your feedback and concerns.

Correction to my previous post:
The Authentication package is not required when using Analytics.
All that is required is the Import to Core : using Unity.Services.Core;

Once you have imported the package you must include the following import/using statement

using Unity.Services.Analytics;
using Unity.Services.Core;

Here is the full script that can be attached to a game object in your scene as an example.
If you have created a custom event, Simply replace the parameter and event with your own

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Unity.Services.Analytics;
using Unity.Services.Core;

public class UGS_Analytics : MonoBehaviour
{
    async void Start()
    {

        await UnityServices.InitializeAsync();
   
        HandleCustomEvent();
    }

    private void HandleCustomEvent()
    {
        //Handle Custom event
        Dictionary<string, object> parameters = new Dictionary<string, object>()
        {
            { "myCustomParam", "Hello World"}
        };
        // The event will get queued up and sent every minute
        Events.CustomData("myCustomEvent", parameters);
        // You can call Events.Flush() to send the event immediately
        Events.Flush();
    }
}

i started from here,

the big note on the first line made me think that its required to signup for that beta, to use those events,
then went to signup on dashboard, followed to installation guide:
https://docs.unity.com/analytics/SDKInstallation.html
selected analytics, made the snippet, it generated only that “com.unity.services.analytics” line.

Hi mgear, good shout I have corrected my initial response. Please let me know if inserting the following line in your code solves your issue

using Unity.Services.Core;

Not working at 2020.3.23f1, the Unity.Services.Core is not available.

Please show a screenshot of your Package Manager showing the “Analytics” package as being installed, and the version.

Hi! I’m stuck with the very similar problem :frowning:
Unity 2020.3.25f1 and here my screens:

The first one - your sample code,
the second - my packages,
and the last one - my assembly file

I tried to install and uninstall the Analytics plugin. Didn’t work.



Can you try with a new/empty project with no assembly files, as a test?

Yes. I’ll try this right now and will be back soon with the result

UPD: it’s ok, with an empty project and Analytics plugin. well, it’s exciting…

…and it’s done. I found a solution.
In my case, I missed the reference to the “Unity.Services.Core” assembly.
Now everything works fine. Thanks!

6 Likes

We’re having a similar problem with missing namespace. We have Analytics Library 3.6.12 and the built-in Unity Analytics. The Unity.Services.Core namespace seems OK but the Analytics one still errors.

We’re on Unity 2021.3.1f1.
(We don’t see the version 4.1.0 and the package name is ‘Analytics library’, not ‘Analytics’.

Can anyone help us with this? @JeffDUnity3D @Elledan3101
8349312--1098600--AnalyticsPackage.png

Thank you!

1 Like

This should help. Some versions of Unity have not received a certain Package Manager backport, so you need to add the package manually https://forum.unity.com/threads/analytics-package-not-showing-up-in-package-manager.1315419/#post-8320653

Edit: Importing the Samples seems to have solved it - weird.

Thanks @JeffDUnity3D . We removed ‘Unity Analytics’ and manually add ‘Analytics’ but the same problem persists.

I tried leaving them both installed but namespace still unavailable. Any other ideas?

I trust you restarted Unity, etc. Please try with a new project. You may need to regenerate your project files on this project. Does the code run in Unity? That is, is this only an Intellisense issue in your code editor? Perhaps I misunderstood, your first sentence says you solved the issue.

Yes sorry for the misunderstanding. The ‘EDIT’ came after so the problem is now resolved.

Thanks again.

Hello Everyone, I can’t seem to find unity analytics 4.1. for some reason the only package I can download is Analytics Library 3.6.12. Also, in the service tab I can only see Legacy Analytics, how can I set up services so I canturn on Analytics?

Please refer to this previous post above, you need to add it manually. Some versions of Unity did not receive the corresponding Package Manager back port https://discussions.unity.com/t/874700/13

“Had the same with 2021.3.0f1, I had to manually add the package by name (+ sign in the top left corner) and add com.unity.services.analytics”

same problem goes…

@hexdecc you are replying to quite an old thread, versions have moved on a bit since the previous posts.

  • Legacy Analytics (e.g. Analytics Library 3.6.12) has been sunset and replaced with Unity Anlaytics.
  • The current version of the Unity Analytics package is 5.1.0 (com.unity.services.analytics). It can be Installed from the Package Manager.

Here’s a link to the Unity Analytics documentation.