Also getting the namespace error

Hi team,

Getting the following error in VS.
|Error|CS0234|The type or namespace name ‘EditorSnapSettings’ does not exist in the namespace ‘UnityEditor’ (are you missing an assembly reference?)|

Seems some people managed to get rid of it by deleting all of the csproj and sln files and then reopening the project.

This did not work for me, any other ideas?

Also the code works fine, im just gonna ignore the errors for now so I can continue.

Thanks

Hi Thomas,

Which version of Unity and which script editor do you use?

If you use VS Code, please follow the instruction on this website and make sure all required extensions are installed.

Also install the Visual Studio Code Editor package in Window > Package Manager in Unity.

Check the console in VS Code (not Unity!). If the .NET Framework 4.7.1 is mentioned, download the .NET Framework 4.7.1 (Developer Pack) from the official Microsoft website and install it. Here is the link:
https://dotnet.microsoft.com/download/dotnet-framework/net471
Maybe you’ll have to reboot your computer. Then launch Unity again and open one of your scripts.

Did this fix it?


See also:

Hi Nina,

I am using unity 2020.3.14f1

and Microsoft Visual Studio Community 2019
Version 16.10.4

Try to wrap the namespace at the top of your code in the lines with #:

#if UNITY_EDITOR
using UnityEditor;
#endif

If you don’t have the namespace at the top of your code, just paste the three lines.

Did this work? If not, please share your script here as formatted text and let me know in which folder the file is.


See also:

HI Nina,

Thanks for your help :slight_smile:

Here is the code.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;
[ExecuteAlways]
public class CoordinateLabeler : MonoBehaviour
{
    TextMeshPro label;
    Vector2Int coordinates = new Vector2Int();
     void Awake()
    {
        label = GetComponent<TextMeshPro>();
        DisplayCoordinates();
    }
    // Update is called once per frame
    void Update()
    {
        if(!Application.isPlaying)
        {
            DisplayCoordinates();
            UpdateObjectName();
        }

    }

    void DisplayCoordinates()
    {
        // coordinates is a 2D vector, so Y in coordinates is equal to parents Z transform
        coordinates.x = Mathf.RoundToInt(transform.parent.position.x / UnityEditor.EditorSnapSettings.move.x);
        coordinates.y = Mathf.RoundToInt(transform.parent.position.z / UnityEditor.EditorSnapSettings.move.z);
        label.text = coordinates.x + "," + coordinates.y;   
    }

    void UpdateObjectName()
    {
        transform.parent.name = coordinates.ToString();
    }
}

Thank you. Try the following:

// in your method
 void DisplayCoordinates()
    {
        // coordinates is a 2D vector, so Y in coordinates is equal to parents Z transform
        #if UNITY_EDITOR
        coordinates.x = Mathf.RoundToInt(transform.parent.position.x / EditorSnapSettings.move.x);
        coordinates.y = Mathf.RoundToInt(transform.parent.position.z / EditorSnapSettings.move.z);
        #endif

        label.text = coordinates.x + "," + coordinates.y;   
    }

Hi NINA,

this has eliminated the problem. Thankyou.

What is happening in your example?

Thanks
Thomas

The EditorSnapSettings namespace cannot be accessed during runtime. We need to tell the compiler that this is code for the editor only not for the actual game. Unfortunately, I haven’t figured out yet why the “normal” code does not work for some students. Maybe it’s due to their version of Unity or there is a “secret” option somewhere in the settings which has not been enabled. At least, #if UNITY_EDITOR seems to solve the problem. :slight_smile: