Question by
sanschaise · Jul 01, 2020 at 02:43 PM ·
dictionarycache
Can't find Dictionary value inside a specific function
I don't know if this is a horrible way of doing things but it has been a fast method that I like. Within my UI root I cache all into dictionaries at Awake that start with a "#" and sometimes "@"
private Dictionary<string, Transform> _ui = new Dictionary<string, Transform>();
void CacheUIandTweens()
{
foreach (Transform child in UIRoot.GetComponentsInChildren<Transform>(true)) //include inactive
{
if (child.name.StartsWith("#"))
{
_ui.Add(child.name.Trim(), child);
}
}
}
This has worked great for the most part but now I'm having trouble accessing a specific value within a function I call after a button press. ( not at start )
The strangest part is that I can get that value in other parts of the code. Any ideas? This is the error:
KeyNotFoundException: The given key was not present in the dictionary.
System.Collections.Generic.Dictionary`2[TKey,TValue].get_Item (TKey key) (at <fb001e01371b4adca20013e0ac763896>:0)
UIManager.UpgradeTowerBtn () (at Assets/_Scripts/_Managers/UIManager.cs:138)
UnityEngine.Events.InvokableCall.Invoke () (at /Users/builduser/buildslave/unity/build/Runtime/Export/UnityEvent/UnityEvent.cs:180)
UnityEngine.Events.UnityEvent.Invoke () (at /Users/builduser/buildslave/unity/build/Runtime/Export/UnityEvent/UnityEvent/UnityEvent_0.cs:58)
UnityEngine.UI.Button.Press () (at /Applications/Unity/Hub/Editor/2020.1.0b12/Unity.app/Contents/Resources/PackageManager/BuiltInPackages/com.unity.ugui/Runtime/UI/Core/Button.cs:68)
UnityEngine.UI.Button.OnPointerClick (UnityEngine.EventSystems.PointerEventData eventData) (at /Applications/Unity/Hub/Editor/2020.1.0b12/Unity.app/Contents/Resources/PackageManager/BuiltInPackages/com.unity.ugui/Runtime/UI/Core/Button.cs:110)
UnityEngine.EventSystems.ExecuteEvents.Execute (UnityEngine.EventSystems.IPointerClickHandler handler, UnityEngine.EventSystems.BaseEventData eventData) (at /Applications/Unity/Hub/Editor/2020.1.0b12/Unity.app/Contents/Resources/PackageManager/BuiltInPackages/com.unity.ugui/Runtime/EventSystem/ExecuteEvents.cs:50)
UnityEngine.EventSystems.ExecuteEvents.Execute[T] (UnityEngine.GameObject target, UnityEngine.EventSystems.BaseEventData eventData, UnityEngine.EventSystems.ExecuteEvents+EventFunction`1[T1] functor) (at /Applications/Unity/Hub/Editor/2020.1.0b12/Unity.app/Contents/Resources/PackageManager/BuiltInPackages/com.unity.ugui/Runtime/EventSystem/ExecuteEvents.cs:261)
UnityEngine.EventSystems.EventSystem:Update() (at /Applications/Unity/Hub/Editor/2020.1.0b12/Unity.app/Contents/Resources/PackageManager/BuiltInPackages/com.unity.ugui/Runtime/EventSystem/EventSystem.cs:376)
Comment
Your answer