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.
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.
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();
}
}
// 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;
}
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.