Hello, I have created a GridController that stores and modifies an array of constructed “Nodes” and a List. Each node stores a Vector3 intended to be accessed by other gameobjects (enemies). The GridController itself is perfectly functional, using the methods within Update() functions without issue.
Here’s were things get weird. Despite everything working perfectly, whenever an enemy tries to use these same methods, the code behaves as if there is nothing in either of the array/lists at all, both giving a length/count of 0. Again, there are no issues when doing the same within the Update() of the GridController, these only occur with enemy gameobjects. It can’t be that the enemies are accessing something that hasn’t been created yet, the GridController is initialized well before the enemies are even spawned.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
// node that stores one space in the "grid" as well as its current vacancy (might be useless but it's there just in case)
public class Node
{
private Vector3 position;
private bool isOccupied;
public Node(Vector3 newPos)
{
position = newPos;
isOccupied = false;
}
public Vector3 GetPosition()
{
return position;
}
public bool IsVacant()
{
return !isOccupied;
}
}
// creates a list of nodes, as well as a list of nodes that are currently free; also contains functions for managing these lists
public class GridController : MonoBehaviour
{
private Node[] nodes;
private List<int> freeNodes; // list of numbers from 0 to x, pick one at random and then plug it into nodes for a free position
// Start is called before the first frame update
void Awake()
{
// the list of nodes and positions
nodes = new Node[12];
nodes[0] = new Node(new Vector3(-3.2f, -0.7f, 0.0f));
nodes[1] = new Node(new Vector3(-1.3f, -0.7f, 0.0f));
nodes[2] = new Node(new Vector3(1.3f, -0.7f, 0.0f));
nodes[3] = new Node(new Vector3(3.2f, -0.7f, 0.0f));
nodes[4] = new Node(new Vector3(-3.2f, -3.4f, 0.0f));
nodes[5] = new Node(new Vector3(-1.3f, -3.4f, 0.0f));
nodes[6] = new Node(new Vector3(1.3f, -3.4f, 0.0f));
nodes[7] = new Node(new Vector3(3.2f, -3.4f, 0.0f));
nodes[8] = new Node(new Vector3(-3.2f, -5.7f, 0.0f));
nodes[9] = new Node(new Vector3(-1.3f, -5.7f, 0.0f));
nodes[10] = new Node(new Vector3(1.3f, -5.7f, 0.0f));
nodes[11] = new Node(new Vector3(3.2f, -5.7f, 0.0f));
freeNodes = new List<int>();
Debug.Log("node number 3's position is " + nodes[2].GetPosition());
// add the (currently) free nodes to the other list
for (int i = 0; i < 12; i++)
{
freeNodes.Add(i);
}
Debug.Log("Free nodes currently contains " + freeNodes.Count + " nodes.");
}
/* this works apparently
void Update()
{
int lmao = GetRandomFreeNode();
Debug.Log("I chose: " + lmao.ToString());
FreeUpFreeNode(lmao);
}
*/
// public functions to be accessed by enemies and modify the free node list
// will remove a given int from the list, meaning it's respective node is now "occupied"
public void OccupyFreeNode(int num)
{
freeNodes.Remove(num);
}
// will add a given int to the list, meaning it's respective node is now free.
// note: intended to recieve the enemy's stored int of its current node, meaning the list can never have duplicate numbers/etc
public void FreeUpFreeNode(int num)
{
freeNodes.Add(num);
}
// will return a free position for an enemy while removing it from its list
public int GetRandomFreeNode()
{
int temp;
/*
if (freeNodes.Count != 0)
temp = freeNodes[Random.Range(0, freeNodes.Count)];
else
temp = -1; // intentional game breaking error to let me know i screwed up
*/
Debug.Log("Free nodes currently contains " + freeNodes.Count + " nodes.");
temp = freeNodes[Random.Range(0, freeNodes.Count)];
OccupyFreeNode(temp);
return temp;
}
// will return the Vector3 of the given index
public Vector3 GetNode(int num)
{
Debug.Log("I tried to access " + num.ToString());
return nodes[num].GetPosition();
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class EnemyBrain : MonoBehaviour
{
private bool scary; // can the enemy damage the player
private int currentPos; // the current target/hover point for this enemy
public float speed; // how fast can the enemy move into position?
public GridController mothership; // just a nick name for the grid positioning system
// Start is called before the first frame update
void Start()
{
scary = false;
currentPos = mothership.GetRandomFreeNode();
Debug.Log("I just chose node " + currentPos.ToString());
}
// Update is called once per frame
void Update()
{
transform.position = Vector3.MoveTowards(transform.position, mothership.GetNode(currentPos), speed * Time.deltaTime);
}
public bool IsScary()
{
return scary;
}
}
I’m also providing the error stacks. Please help!

