unitedinc
unitedinc

Reputation: 67

My C# Dictionary only has Null values in it

I am creating a C# Dictionary within Unity to house the states of my Finite State Machine, as listed below.

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

public class AmalgamCentralAI : MonoBehaviour
{
    private float stunHealth;

    public bool playerInSight;

    public Transform interuptedEvent;

    public GameObject playerTracker;

    public Transform[] leavingAreas;

    private int tensionMeter;

    public AmalgamFSM FSMLogic;

    private void Start()
    {
        interuptedEvent = null;
        FSMLogic = GetComponent<AmalgamFSM>();
        Dictionary<Type, EmptyState> states = new Dictionary<Type, EmptyState>
        {
            {typeof(RoamingState), new RoamingState() },
            {typeof(IdleState), new IdleState() },
            {typeof(StalkingState), new StalkingState() },
            {typeof(HuntingState), new HuntingState() },
            {typeof(HauntingState), new HauntingState() },
            {typeof(LeavingState), new LeavingState() }
        };
        Debug.Log(states);
        FSMLogic.GrabAllStates(states);
    }

    private void Update()
    {
        trackPlayerVisability();
    }

    private void trackPlayerVisability()
    {
        RaycastHit hit;
        if (Physics.Raycast(transform.position, playerTracker.transform.position, out hit, Mathf.Infinity))
        {
            if(hit.transform.tag == "Player")
            {
                playerInSight = true;
            }
            else
            {
                playerInSight = false;
            }
        }
    }
    
}

Unfortunately the only valuables of the dictionary afterwards only contain Null values. Rather than the states. All the states are child classes of the EmptyState, and all FSMLogic.GrabAllStates does is copy the contents to another script.

Upvotes: 1

Views: 55

Answers (0)

Related Questions