- Home /
KeyNotFoundException: The given key was not present in the dictionary.
i have no Idea what is going on. It was working perfectly fine then I added a few more achievements and now i get the error. Please help I've spent hours on this problem and I'm really not sure what to do at this point.
It only lets me instantiate the first 11 on load. Also It starts with the Achievement Menu active, rather than waiting for input "I" before opening.
Thank you all for your time!
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; using UnityEngine.SceneManagement;
public class AchevemntManager : MonoBehaviour {
public GameObject achievmentPrefab;
public GameObject achivementMenu;
public Sprite[] sprites;
public GameObject visualAchievment;
public Dictionary<string, Achevment> achivements = new Dictionary<string, Achevment> ();
public Sprite unlockedSprite;
private int fadeTime = 2;
private static AchevemntManager instance;
public static AchevemntManager Instance
{
get
{
if (instance == null)
{
instance = GameObject.FindObjectOfType<AchevemntManager> ();
}
return AchevemntManager.instance;
}
}
// Use this for initialization
void Start () {
PlayerPrefs.DeleteAll ();
//achivements.Add("FirstBloodAchevment",new Achevment("First Blood", "Made your first kill huh? How'd it feel?" this));
CreateAchievment ("General","Sympathizer","Talk to a monster...They can't be that bad, right?",0);
CreateAchievment ("General","Poor Alex","See Alex at her worst.",1);
CreateAchievment ("General","Poor, Poor Alex","See Alex at her worst, twice.",2);
CreateAchievment ("General","Cat Whisperer","Talk to a cat you didn't have to talk to.",3);
CreateAchievment ("General","The Hostel","Defeat the Great Beast in the Hostel Halls. Did you run or did you fight?",4);
CreateAchievment ("General","Dream Walker","Defeat the Beast that lives in your Dreams. Did you run or fight?",5);
CreateAchievment ("General","Hell Spawn","Defeat the Beast that stirs in the depths. Did you run or fight?",6);
CreateAchievment ("General","Smoke and Ash","Discover the Good Ending.",7);
CreateAchievment ("General","Last Rites","Discover the Bad Ending.",8);
CreateAchievment ("General","Five More Minutes","Discover a Passive Ending.",9);
CreateAchievment ("General","The Sad Truth","Discover the all of the endings.",10, new string[]{"Smoke and Ash","Last Rites","The Broken"});
CreateAchievment ("General","The Elder","Follow the Candle.",11);
CreateAchievment ("General","The Beast","Follow the Cane.",12);
CreateAchievment ("General","The Sacrifice","Follow the Blade.",13);
CreateAchievment ("General","The Abused","Follow the Phone.",14);
CreateAchievment ("General", "The Butterfly", "Follow the Cuff.", 15);
CreateAchievment ("General","The Punished","Follow the Car.",16);
CreateAchievment ("General","The Whole Truth","Discover the whole story.",17, new string[]{"The Elder","The Beast","The Sacrifice","The Abused","The Butterfly","The Punished","The Sad Truth","The Broken"});
CreateAchievment ("General","The Fool","Put ??? to Sleep.",18);
CreateAchievment ("General","The Playground","Put Marchosias to Sleep.",19);
CreateAchievment ("General","The Failure","Put Lucas to Sleep.",20);
CreateAchievment ("General","The Tortured","Put Alex to Sleep.",21);
CreateAchievment ("General","The Justifier","Put Amy to Sleep.",22);
CreateAchievment ("General","The Wrathful","Put Daudaz to Sleep.",23);
CreateAchievment ("General","The Broken","Discover all Passive Endings.",24, new string[]{"The Fool","The Playground","The Failure","The Tortured","The Justifier","The Wrathful"});
CreateAchievment ("General","First Step","The Longest Journey Starts With a Single Step.",25);
achivementMenu.SetActive (false);
}
// Update is called once per frame
void Update ()
{
if (Input.GetKeyDown (KeyCode.I))
{
achivementMenu.SetActive (!achivementMenu.activeSelf);
}
if (Input.GetKeyDown (KeyCode.D))
{
EarnAchivment("First Step");
}
/*if (Input.GetKeyDown (KeyCode.O))
{
EarnAchivment("Sympathizer");
}
if (Input.GetKeyDown (KeyCode.P))
{
EarnAchivment("Poor Alex");
}
if (Input.GetKeyDown (KeyCode.L))
{
EarnAchivment("Poor, Poor Alex");
}
if (Input.GetKeyDown (KeyCode.L))
{
EarnAchivment("Temple Runner");
}
if (Input.GetKeyDown (KeyCode.L))
{
EarnAchivment("The Hostel");
}
if (Input.GetKeyDown (KeyCode.L))
{
EarnAchivment("Dream Walker");
}
if (Input.GetKeyDown (KeyCode.L))
{
EarnAchivment("Hell Spawn");
}
if (Input.GetKeyDown (KeyCode.L))
{
EarnAchivment("Smoke and Ash");
}
if (Input.GetKeyDown (KeyCode.L))
{
EarnAchivment("Last Rites");
}
if (Input.GetKeyDown (KeyCode.L))
{
EarnAchivment("Five More Minutes");
}
if (Input.GetKeyDown (KeyCode.L))
{
EarnAchivment("The Sad Truth");
}
if (Input.GetKeyDown (KeyCode.L))
{
EarnAchivment("The Elder");
}
if (Input.GetKeyDown (KeyCode.L))
{
EarnAchivment("The Elder");
}
if (Input.GetKeyDown (KeyCode.L))
{
EarnAchivment("The Beast");
}
if (Input.GetKeyDown (KeyCode.L))
{
EarnAchivment("The Sacrifice");
}
if (Input.GetKeyDown (KeyCode.L))
{
EarnAchivment("The Abused");
}
if (Input.GetKeyDown (KeyCode.L))
{
EarnAchivment("The Butterfly");
}
if (Input.GetKeyDown (KeyCode.L))
{
EarnAchivment("The Punished");
}
if (Input.GetKeyDown (KeyCode.L))
{
EarnAchivment("The Whole Truth");
}*/
}
public void EarnAchivment(string title)
{
if (achivements [title].earnAchievment ())
{
GameObject achievment = (GameObject)Instantiate (visualAchievment);
SetAchievmentInfo("EarnCanvis", achievment, title);
StartCoroutine (FadeAchivement (achievment));
}
}
public IEnumerator HideAchivement(GameObject achievment)
{
yield return new WaitForSeconds (3);
Destroy (achievment);
}
public void CreateAchievment (string parent, string title, string description, int spriteIndex, string[] dependecies = null)
{
GameObject achievment = (GameObject)Instantiate (achievmentPrefab);
Achevment newAchevment = new Achevment (name, description, spriteIndex, achievment);
achivements.Add (title, newAchevment);
SetAchievmentInfo (parent, achievment, title);
if (dependecies != null)
{
foreach (string achivementTitle in dependecies)
{
Achevment dependency = achivements [achivementTitle];
dependency.Child = title;
newAchevment.AddDependency (achivements [achivementTitle]);
}
}
}
public void SetAchievmentInfo(string parent, GameObject achievent,string title)
{
achievent.transform.SetParent(GameObject.Find(parent).transform);
achievent.transform.localScale = new Vector3 (1, 1, 1);
achievent.transform.GetChild(0).GetComponent<Text>().text = title;
achievent.transform.GetChild(1).GetComponent<Text>().text = achivements[title].Description;
achievent.transform.GetChild(2).GetComponent<Image>().sprite = sprites[achivements[title].SpriteIndex];
}
private IEnumerator FadeAchivement(GameObject achievent)
{
CanvasGroup canvasGroup = achievent.GetComponent<CanvasGroup> ();
float rate = 1.0f / fadeTime;
int startAlpha = 0;
int endAlpha = 1;
for (int i = 0; i < 2; i++)
{
float progress = 0.0f;
while (progress < 1.0)
{
canvasGroup.alpha = Mathf.Lerp (startAlpha, endAlpha, progress);
progress += rate * Time.deltaTime;
yield return null;
}
yield return new WaitForSeconds (2);
startAlpha = 1;
endAlpha = 0;
}
Destroy (achievent);
}
}
Your answer
Follow this Question
Related Questions
The given key was not present in the dictionary, i am calling the key immediately after adding it 1 Answer
Dictionary within a dictionary? 1 Answer
ES2 Can't save Dictionary 1 Answer
Dictionary.TryGetValue throwing an error DivideByZeroException: Division by zero 2 Answers
KeyNotFoundException that makes no sense (*I* can find the key) 0 Answers