I keep getting a null reference exception when I try to reference my character manger script in my testing script
Character manger:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
/// <summary>
/// Responsibvle for adding and manging chacters in a scene
/// </summary>
public class CharcterManger : MonoBehaviour
{
public static CharcterManger instance;
/// <summary>
/// All chacters must be attached to the charcter pannel
/// </summary>
public RectTransform CharactersPannel;
/// <summary>
/// a list of every chacter in the scene
/// </summary>
public List<Character> characters = new List<Character>();
/// <summary>
/// easy look up for our chacters
/// </summary>
public Dictionary<string, int> characterDictionary = new Dictionary<string, int>();
void awake ()
{
instance = this;
}
/// <summary>
/// try to get charcter from the list
/// </summary>
/// <param name="charactersName"></param>
public Character getCharacter(string charactersName, bool createCharacterIfDoesNotExist = true)
{
//search our libary to see if a chacter is alredy in the scene
int index = -1;
if (characterDictionary.TryGetValue(charactersName, out index))
{
return characters[index];
}
else if(createCharacterIfDoesNotExist)
{
return createCharacter(charactersName);
}
return null;
}
/// <summary>
/// create the chacter
/// </summary>
/// <param name="characterName"></param>
/// <returns></returns>
public Character createCharacter(string characterName)
{
Character newCharacter = new Character(characterName);
characterDictionary.Add (characterName, characters.Count);
characters.Add(newCharacter);
return newCharacter;
}
}
Testing:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ChacterTesting : MonoBehaviour
{
public Character Reinart;
public Character Lynex;
// Start is called before the first frame update
void Start()
{
Reinart = CharcterManger.instance.getCharacter("Reinart");
Lynex = CharcterManger.instance.getCharacter("Lynex");
}
}