I have a ScriptableObject that holds the data that I will put into a Dictionary:
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Audio;
using Unity.VisualScripting;
[CreateAssetMenu(fileName = "New PronunciationData", menuName = "Pronunciation Data", order = 51)]
public class PronunciationData : ScriptableObject
{
[SerializeField]
public BlendKey thisBlendKey;
[IncludeInSettings(true)]
[System.Serializable]
public class BlendKey{
[SerializeField]
public string soundName;
[SerializeField]
public List<NeemData> neemList;
}
[SerializeField]
public AudioClip Pronunciation;
}
BlendKey will be the key for the Dictionary. It's a custom class which contains 2 items:
- A string (e.g. the letters used to make a particular sound)
- A list of items in another custom class called NeemData (e.g. a list of the sounds themselves).
NeemData is defined in another Scriptable Object. For the purposes of comparing 2 NeemDatas, I only really need to confirm that neemName is the same for both:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Audio;
[CreateAssetMenu(fileName = "New NeemData", menuName = "Neem Data", order = 51)]
public class NeemData : ScriptableObject
{
[SerializeField]
public string neemName;
[SerializeField]
public Vector3 neemPosition;
[SerializeField]
public Sprite singleNeemSprite;
[SerializeField]
public Color baseColor;
[SerializeField]
public AudioClip Pronunciation;
[SerializeField]
public AudioClip placementAudio;
[SerializeField]
public List<PronunciationData> blendPronunciation;
[SerializeField]
public string feemText;
[SerializeField]
public List<FeemData> coreFeems;
[SerializeField]
public Color feemColor;
}
Pronunciation is just an Audioclip, and will be the value for the Dictionary.
The problem is that no matter what BlendKey I provide, the Dictionary returns a value of Null. In other words, it's unable to find anything using the keys I provide - even though I've done extensive checking to make sure that the key is exactly the same.
I think there are 2 possible causes:
I'm using a custom class as a Key. Some sources mention that when you do so, you "need to override GetHashCode() (and preferably also Equals())". https://stackoverflow.com/questions/6999191/use-custom-object-as-dictionary-key
One of the items in my Custom Class is a List. I've encountered problems comparing Lists before for a match, so imagine it would be even more tricky to do so here... where you have to match not only 2 lists, but also the 2 other strings (i.e. the soundNames).
I've looked into other possible data structures, but the challenge is that I really do need both the SoundName and the neemList to be a unique combination. So the Dictionary Key really does need to use both of those variables.
Is it possible to edit the Scriptable Object to use overrides, such that Unity can successfully look up values in the dictionary? Please let me know - thank you!
I really do need both the BlendKey and the SoundName
it looks to me that SoundName is already part of Blendkey. \$\endgroup\$