GameObject.FindGameObjectsWithTag passing null reference and Dictionary not created problem

Hi, everybody.

Im creating a simulation environment, where 5 stations are randomly place in an area and 2 obstacles are spawned with random size and random velocity to move around, blocking connections between stations.

I have a GameManager which controls the spawn of stations and obstacles (in SpawnManager.cs) and controls the connections between each stations (in ConnectionManager.cs).

So the logic of the this is SpawnManager spawn stations and obstacles, after spawning, pass all the stations (using GameObject.FindGameObjectsWithTag() method) to ConnectionManager. And it here that i get the NullRefenceException error. This error is kinda tricky, the Unity editor reports this error and the message points the error occured in ConnectionManager.cs where statusMap.Add(connectionLines[i, j].gameObject.name, 0) is called. But when double tap this error the editor jumps to SetStations(GameObject[] stations)in SpawnManager.cs.

And in ConnectionManager.cs , i used a Dictionary<string, int> to keep track all the connections between each station. All the connections are GameObjects different from station objects. The key (string) is the name of the connectino object and the value (int) is the connection status (0 - available, 1 - connected, 2 - blocked). When DrawConnection(statusMap[connectionLines[i, j].gameObject.name], i, j) in ConnectionManager.cs is called, it throws out the KeyNotFoundException, but im sure it definitely there. I checked with Dictionary.Keys and there is a string with the object name.

So anyone could help me with my code here? Any help would be appriciated!

SpawnManager.cs

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class SpawnManager : MonoBehaviour
{
    public GameObject[] stationSpawnArea = new GameObject[5];
    public GameObject station;
    public GameObject[] obstacleSpawnArea = new GameObject[2];
    public GameObject obstacle;
    public ConnectionManager cm;

    public int stationCount = 5;
    public int obstacleCount = 2;
    public int obstaclMinScale = 2;
    public int obstacleMaxScale = 5;
    public float obstacleMinVelocity = 1.0f;
    public float obstacleMaxVelocity = 3.0f;

    // Start is called before the first frame update
    void Start()
    {
        /* Some code set up stations
        ...
        */
        /*  Some code set up obstacles
        ...
       */
        cm = GetComponent<ConnectionManager>();
        GameObject[] temp = GameObject.FindGameObjectsWithTag("Station");
        Debug.Log(temp.Length); // It returns 5
        cm.SetStations(temp); // Here throws NullReferenceException
    }

    // Update is called once per frame
    void Update()
    {
        
    }
}

ConnectionManager.cs

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ConnectionManager : MonoBehaviour
{
    public GameObject[] stations; // Stores all the station GameObjects
    public GameObject[,] connectionLines; // Stores all the connection line GameObjects
    public Material mat;
    public float lineWidth = 0.3f;
    public Dictionary<string, int> statusMap;
    public HashSet<GameObject> onComSet;

    // Start is called before the first frame update
    void Start()
    {
        statusMap = new Dictionary<string, int>();
        onComSet = new HashSet<GameObject>(); // Store the connections set to 'Connected'
    }

    // Update is called once per frame
    void Update()
    {
        UpdateAllConnection();
    }
    public void SetStations(GameObject[] s){
        stations = new GameObject[s.Length];
        s.CopyTo(stations, 0);
        connectionLines = new GameObject[s.Length, s.Length];
        for(int i = 0; i < s.Length - 1; i++)
            for(int j = i + 1; j < s.Length; j++){
                /* Some codes setting up components of connection lines
               ...
               */
                // Below is the NullReferenceException error message points to be
                statusMap.Add(connectionLines[i, j].gameObject.name, 0); // Set the connection available
                foreach(var key in statusMap.Keys)
                    Debug.Log(key);
            }
    }
    /* Some code setting the connections
    */
    public void UpdateAllConnection(){
        //connectionMap
        Debug.Log(stations.Length);
        for(int i = 0; i < stations.Length - 1; i++)
            for(int j = i + 1; j < stations.Length; j++){
                DrawConnection(statusMap[connectionLines[i, j].gameObject.name], i, j); // Here throws the KeyNotFoundException
            }
    }
    public void DrawConnection(int status, int station1idx, int station2idx){
        // Available
        if(status == 0){
            LineRenderer line = connectionLines[station1idx, station2idx].GetComponent<LineRenderer>();
            line.startColor = Color.gray;
            line.endColor = Color.gray;
        }
        // Connected
        if(status == 1){
            LineRenderer line = connectionLines[station1idx, station2idx].GetComponent<LineRenderer>();
            line.startColor = Color.green;
            line.endColor = Color.green;
        }
        // Blocked
        if(status == 2){
            LineRenderer line = connectionLines[station1idx, station2idx].GetComponent<LineRenderer>();
            line.startColor = Color.red;
            line.endColor = Color.red;
        }
    }
}

And again, thanks for any kinds of help!

Quick update for this question:

I have found that the Dictionary.Add(string, int) method isn’t adding any entry to the dict, nor the Dictionary[key] = value. And i also tried to use

if(Dictionary.TryAdd(string, int))
    Debug.Log("success");

to check if successfully added to the dictionary but no… Could anyone tell me why???

Ok, solution found! It’s the difference between Start() and Awake()!!! How stupid i am~