Seem a bit too late to question this, and i know what it does, I’ve spent some months studying C# and usually constructors and instantiating those with the “new” keyword to initialize them is very common, but i just struggle to understand when it is the case to use it and especially in Unity scripts, where monobehaviours seems do not need most of the time.
For example here, in the code from Gary lecture:
TextMeshPro label;
Vector2Int coordinates = new Vector2Int();
void Awake()
{
label = GetComponent<TextMeshPro>();
DisplayCoordinates();
}
void Update()
{
if(!Application.isPlaying)
{
DisplayCoordinates();
UpdateObjectName();
}
}
void DisplayCoordinates()
{
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();
}
I would never guessed that we need to initialize coordinates as a new Vector2Int.
Anyone have a simple explanation to help me understand better when you have to do it and when you don’t? Thanks in advance