I did as you said and now I do have an error popping up, but still no movement.
NullReferenceException: Object reference not set to an instance of an object
GridSystemVisual.UpdateGridVisual () (at Assets/Scripts/Grid/GridSystemVisual.cs:172)
GridSystemVisual.Start () (at Assets/Scripts/Grid/GridSystemVisual.cs:69)
Here is my Pathnode and GridVisual
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Pathnode
{
private GridPosition gridPosition;
private int gCost;
private int hCost;
private int fCost;
private Pathnode cameFromPathnode;
private bool isWalkable = true;
public Pathnode(GridPosition gridPosition)
{
this.gridPosition = gridPosition;
}
public override string ToString()
{
return gridPosition.ToString();
}
public int GetGCost()
{
return gCost;
}
public int GetHCost()
{
return hCost;
} public int GetFCost()
{
return fCost;
}
public void SetGCost(int gCost)
{
this.gCost = gCost;
}
public void SetHCost(int hCost)
{
this.hCost = hCost;
}
public void CalculateFCost()
{
fCost = gCost + hCost;
}
public void ResetCameFromPathnode()
{
cameFromPathnode = null;
}
public GridPosition GetGridPosition()
{
return gridPosition;
}
public void SetCameFromPathnode(Pathnode pathnode)
{
cameFromPathnode = pathnode;
}
public Pathnode GetFromPathNode()
{
return cameFromPathnode;
}
public bool IsWalkable()
{
return isWalkable;
}
public void SetIsWalkable(bool isWalkable)
{
this.isWalkable = isWalkable;
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
public class GridSystemVisual : MonoBehaviour
{
public static GridSystemVisual Instance { get; private set; }
//Creating different colors for the grid display for each individual action
//The struct allows flexibility for color change without having to have multiple serializedfields for each one
[Serializable] //This is used to make the struct show in the editor
public struct GridVisualTypeMaterial
{
public GridVisualType gridVisualType;
public Material material;
}
public enum GridVisualType
{
White,
Blue,
Red,
RedSoft,
Yellow
}
[SerializeField]
private Transform gridSystemVisualSinglePrefab;
[SerializeField]
private List<GridVisualTypeMaterial> gridVisualTypeMaterialList;
private GridSystemVisualSingle[,] gridSystemVisualSingleArray;
private void Awake()
{
//Log this error as a way to avoid making multiple of this gameobject. This is desgined to prevent the game from crashing
if (Instance != null)
{
Debug.LogError("There is more than one gridsystemviaual instance!" + transform + "..." + Instance);
Destroy(gameObject);
return;
}
Instance = this;
}
private void Start()
{
//Setup our array for the grid visual
gridSystemVisualSingleArray = new GridSystemVisualSingle[
LevelGrid.Instance.GetWidth(),
LevelGrid.Instance.GetHeight()
];
//This is calling the passthrough from levelgrid and gridsystem to access the width and height to set all the prefabs on gridpositions
for (int x = 0; x < LevelGrid.Instance.GetWidth(); x++)
{
for (int z = 0; z < LevelGrid.Instance.GetHeight(); z++)
{
GridPosition gridPosition = new GridPosition(x, z);
Transform gridSystemVisualSingleTransform =
Instantiate(gridSystemVisualSinglePrefab, LevelGrid.Instance.GetWorldPosition(gridPosition), Quaternion.identity);
gridSystemVisualSingleArray[x, z] = gridSystemVisualSingleTransform.GetComponent<GridSystemVisualSingle>();
}
}
//Set up the event to update the grid when an action changes not on a constant update
UnitActionSyste.Instance.OnSelectedActionChanged += UniActionSyste_OnSelectedActionChanged;
LevelGrid.Instance.OnAnyUnitMovedGridPosition += LevelGrid_OnAnyUnitMovedGridPosition;
UpdateGridVisual();
}
public void HideAllGridPosition()
{
for (int x = 0; x < LevelGrid.Instance.GetWidth(); x++)
{
for (int z = 0; z < LevelGrid.Instance.GetHeight(); z++)
{
gridSystemVisualSingleArray[x, z].Hide();
}
}
}
//This will show different colors based on the range of the action
private void ShowGridPositionRangeSquare(GridPosition gridPosition, int range, GridVisualType gridVisualType)
{
List<GridPosition> gridPositionList = new List<GridPosition>();
for (int x = -range; x <= range; x++)
{
for (int z = -range; z <= range; z++)
{
//Test for gridposition to create this ciruclar gridposition
GridPosition testGridPosition = gridPosition + new GridPosition(x, z);
//Validate the gridposition first and then do the rest
if(!LevelGrid.Instance.IsValidGridPosition(testGridPosition))
{
continue;
}
gridPositionList.Add(testGridPosition);
}
}
ShowGridPositionList(gridPositionList, gridVisualType);
}
private void ShowGridPositionRange(GridPosition gridPosition, int range, GridVisualType gridVisualType)
{
List<GridPosition> gridPositionList = new List<GridPosition>();
for (int x = -range; x <= range; x++)
{
for (int z = -range; z <= range; z++)
{
//Test for gridposition to create this ciruclar gridposition
GridPosition testGridPosition = gridPosition + new GridPosition(x, z);
//Validate the gridposition first and then do the rest
if(!LevelGrid.Instance.IsValidGridPosition(testGridPosition))
{
continue;
}
int testDistance = (int)(Mathf.Abs(x) + MathF.Abs(z));
if (testDistance > range)
{
continue;
}
gridPositionList.Add(testGridPosition);
}
}
ShowGridPositionList(gridPositionList, gridVisualType);
}
public void ShowGridPositionList(List<GridPosition> gridPositionList, GridVisualType gridVisualType)
{
foreach (GridPosition gridPosition in gridPositionList)
{
gridSystemVisualSingleArray[gridPosition.x, gridPosition.z].Show(GetGridVisualTypeMaterial(gridVisualType));
}
}
private void UpdateGridVisual()
{
HideAllGridPosition();
//Access the unit for this function to work
Unit selectedUnit = UnitActionSyste.Instance.GetSelectedUnit();
BaseAction selectedAction = UnitActionSyste.Instance.GetSelectedAction();
//cycle through colors based on the action with this switch
GridVisualType gridVisualType;
switch(selectedAction)
{
default:
case MoveAction moveAction:
gridVisualType = GridVisualType.White;
break;
case SpinAction spinAction:
gridVisualType = GridVisualType.Blue;
break;
case GrenadeAction grenadeAction:
gridVisualType = GridVisualType.Yellow;
break;
case ShootAction shootAction:
gridVisualType = GridVisualType.Red;
ShowGridPositionRange(selectedUnit.GetGridPostion(), shootAction.GetTheMaxShootDistance(), GridVisualType.RedSoft);
break;
case SwordAction swordAction:
gridVisualType = GridVisualType.Red;
ShowGridPositionRangeSquare(selectedUnit.GetGridPostion(), swordAction.GetMaxSwordDistance(), GridVisualType.RedSoft);
break;
}
ShowGridPositionList(
selectedAction.GetValidActionGridPositionList(), gridVisualType);
}
private void UniActionSyste_OnSelectedActionChanged(object sender, EventArgs e)
{
UpdateGridVisual();
}
private void LevelGrid_OnAnyUnitMovedGridPosition(object sender, EventArgs e)
{
UpdateGridVisual();
}
private Material GetGridVisualTypeMaterial(GridVisualType gridVisualType)
{
foreach(GridVisualTypeMaterial gridVisualTypeMaterial in gridVisualTypeMaterialList)
{
if(gridVisualTypeMaterial.gridVisualType == gridVisualType)
{
return gridVisualTypeMaterial.material;
}
}
Debug.LogError("Could not find GridVisualTypeMaterial for GridVisualType " + gridVisualType);
return null;
}
}