I am making a game in unity, I have a hexgrid on the map at runtime. I also have a gameObject, selectedCharacter, that I am trying to write a pathfinding script for. In my grid script, as soon as this line runs, the error pops up. This line of code is in the start() function of the grid Script.
selectedCharacter.GetComponent<characterPosition>().hexTile_X = (int) selectedCharacter.transform.position.x;
The error is as follows:
[15:58:06] NullReferenceException: Object reference not set to an instance of an object.
The thing is, I have, in the inspector, assigned the selectedCharacter variable to a game object. So I don't understand this error.
The script I'm accessing, characterPosition, is as follows:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class characterPosition : MonoBehaviour
{
public int hexTile_X, hexTile_Y;
public gridScript map;
public List<Node> currentPath = null;
void Update(){
if(currentPath != null){
int curr = 0;
while(curr < currentPath.Count - 1){
Vector3 start = map.TileCoordToWorldCoord(currentPath[curr].x, currentPath[curr].y);
Vector3 end = map.TileCoordToWorldCoord(currentPath[curr+1].x, currentPath[curr+1].y );
Debug.DrawLine(start, end, Color.yellow);
curr++;
}
}
}
}
Thanks.