The preceding implementation works fine when the only objects we want to tie to the grid are children of our ___snap-to-grid___ GameObject. However, if we don't want to require affected objects to be such children, then we can use the singleton pattern to allow a GameObject anywhere in the Hierarchy window to get a reference to the GridGizmo component. Do the following to adapt this recipe to use this approach:
- Update the contents of the GridGizmo.cs #C script class so that it matches the following:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GridGizmo : MonoBehaviour
{
private static GridGizmo _instance = null;
public static void SetInstance(GridGizmo instance) {
_instance = instance;
}
public static GridGizmo GetInstance() {
if(_instance == null){
throw new System.Exception("error - no GameObject has GridGizmo component to snap to ...");
}
return _instance...