Search Unity

  1. We are migrating the Unity Forums to Unity Discussions by the end of July. Read our announcement for more information and let us know if you have any questions.
    Dismiss Notice
  2. Dismiss Notice

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

Discussion in 'Unity Analytics' started by Kilfeather94, Mar 13, 2022.

  1. Kilfeather94

    Kilfeather94

    Joined:
    Feb 8, 2014
    Posts:
    23
    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:


    Code (CSharp):
    1. // Start is called before the first frame update
    2.     async void Start()
    3.     {
    4.         await UnityServices.InitializeAsync();
    5.     }
    6.  
    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?
     
  2. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    9,553
    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..
     
  3. SebT_Unity

    SebT_Unity

    Unity Technologies

    Joined:
    Jun 21, 2021
    Posts:
    283
    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

    Code (CSharp):
    1. using Unity.Services.Analytics;
    2. using Unity.Services.Core;
    3.  
    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

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using Unity.Services.Analytics;
    5. using Unity.Services.Core;
    6.  
    7. public class UGS_Analytics : MonoBehaviour
    8. {
    9.     async void Start()
    10.     {
    11.  
    12.         await UnityServices.InitializeAsync();
    13.    
    14.         HandleCustomEvent();
    15.     }
    16.  
    17.     private void HandleCustomEvent()
    18.     {
    19.         //Handle Custom event
    20.         Dictionary<string, object> parameters = new Dictionary<string, object>()
    21.         {
    22.             { "myCustomParam", "Hello World"}
    23.         };
    24.         // The event will get queued up and sent every minute
    25.         Events.CustomData("myCustomEvent", parameters);
    26.         // You can call Events.Flush() to send the event immediately
    27.         Events.Flush();
    28.     }
    29. }
     
    Last edited: Mar 14, 2022
  4. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    9,553
  5. SebT_Unity

    SebT_Unity

    Unity Technologies

    Joined:
    Jun 21, 2021
    Posts:
    283
    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
    Code (CSharp):
    1. using Unity.Services.Core;
     
  6. FOTA-Developer

    FOTA-Developer

    Joined:
    Jun 20, 2022
    Posts:
    1
    Not working at 2020.3.23f1, the Unity.Services.Core is not available.
     
  7. JeffDUnity3D

    JeffDUnity3D

    Joined:
    May 2, 2017
    Posts:
    14,446
    Please show a screenshot of your Package Manager showing the "Analytics" package as being installed, and the version.
     
  8. Elledan3101

    Elledan3101

    Joined:
    May 24, 2013
    Posts:
    18
    Hi! I'm stuck with the very similar problem :(
    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.
     

    Attached Files:

  9. JeffDUnity3D

    JeffDUnity3D

    Joined:
    May 2, 2017
    Posts:
    14,446
    Can you try with a new/empty project with no assembly files, as a test?
     
  10. Elledan3101

    Elledan3101

    Joined:
    May 24, 2013
    Posts:
    18
    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...
     
    Last edited: Jul 29, 2022
  11. Elledan3101

    Elledan3101

    Joined:
    May 24, 2013
    Posts:
    18
    ...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!
     

    Attached Files:

  12. FlyingSquirrels

    FlyingSquirrels

    Joined:
    Sep 18, 2015
    Posts:
    84
    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
    AnalyticsPackage.png AnalyticsPackage3.png AnalyticsPackage2.png

    Thank you!
     
    timmy2get likes this.
  13. JeffDUnity3D

    JeffDUnity3D

    Joined:
    May 2, 2017
    Posts:
    14,446
    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/ana...g-up-in-package-manager.1315419/#post-8320653
     
  14. FlyingSquirrels

    FlyingSquirrels

    Joined:
    Sep 18, 2015
    Posts:
    84
    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?

    upload_2022-8-9_18-49-49.png
     
  15. JeffDUnity3D

    JeffDUnity3D

    Joined:
    May 2, 2017
    Posts:
    14,446
    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.
     
  16. FlyingSquirrels

    FlyingSquirrels

    Joined:
    Sep 18, 2015
    Posts:
    84
    Yes sorry for the misunderstanding. The 'EDIT' came after so the problem is now resolved.

    Thanks again.
     
  17. Milanese

    Milanese

    Joined:
    Jul 6, 2015
    Posts:
    4
    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?
     
  18. JeffDUnity3D

    JeffDUnity3D

    Joined:
    May 2, 2017
    Posts:
    14,446
    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://forum.unity.com/threads/err...-in-the-current-context.1252221/#post-8349321

    "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"
     
  19. hexdecc

    hexdecc

    Joined:
    Oct 24, 2014
    Posts:
    160
    same problem goes..
     
  20. Laurie-Unity

    Laurie-Unity

    Unity Technologies

    Joined:
    Mar 5, 2020
    Posts:
    227
    @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.

      upload_2024-1-29_8-15-47.png
     
  21. LookDev

    LookDev

    Joined:
    Dec 31, 2019
    Posts:
    5
    using Unity.Services.Core; not fetch first but then I close and reopen the project and then it working !!!