- Home /
Dictionary TryGetValue not finding key, even though it exists
Hello, I have the following monobehaviour attached to my weapon to detect the other collider's physicmaterial; run it in "clips.TryGetValue(x, x)"; outputting to a variable and then calling a method to play the audio while passing in the value found. I have a debug line before the TryGetValue call. This is running. It finds the material successfully. It seems I'm getting an exception error for the key not existing in the dictionary. However, it has been added on awake. I've tried multiple solutions. I'm going to try to print out a list of the keys and their values after I initialize everything right now.
Any help would be great. Thanks! using System.Collections; using System.Collections.Generic; using UnityEngine;
public class Snd_ImpactListener : MonoBehaviour {
public Dictionary<PhysicMaterial, AudioClip> clips = new Dictionary<PhysicMaterial, AudioClip>();
public AudioClip metalImpact;
public AudioClip woodImpact;
public AudioClip fleshImpact;
public PhysicMaterial metalMaterial;
public PhysicMaterial woodMaterial;
public PhysicMaterial fleshMaterial;
private AudioSource source;
private void Start()
{
if (!clips.ContainsKey(metalMaterial))
clips.Add(metalMaterial, metalImpact);
if (!clips.ContainsKey(woodMaterial))
clips.Add(woodMaterial, woodImpact);
if (!clips.ContainsKey(fleshMaterial))
clips.Add(fleshMaterial, fleshImpact);
}
private void OnEnable()
{
if (!GetComponent<AudioSource>())
{
source = gameObject.AddComponent<AudioSource>();
}
else
{
source = GetComponent<AudioSource>();
}
}
private void OnTriggerEnter(Collider collision)
{
PhysicMaterial materialFound = collision.material;
AudioClip clip;
Debug.Log("Hit detected. attempting to play sound ==> " + materialFound.name.ToString());
if (clips.TryGetValue(materialFound, out clip))
{
PlayRequestedAudio(clip);
Debug.Log("Played sound: " + clips[materialFound].name);
}
}
public void PlayRequestedAudio(AudioClip audio)
{
Debug.Log("Playrequestedaudio");
source.PlayOneShot(audio);
}
}
This might be the problem. $$anonymous$$y gameObject for this is initially disabled. Then when there is an animation, it enables at a given frame then disables. Testing now. I'll let you all know.
Isn't it suppose to be collision .shared$$anonymous$$aterial?
Read the doc.
https://docs.unity3d.com/ScriptReference/Renderer-material.html
collsion.material make a copy of new material. So it might not be the same material with dictionary.
you're right. I don't know why I skipped over that. Thanks for this answer. Great job.
Answer by VicciGames · Jul 05, 2018 at 04:05 PM
For anyone wondering about the answer to this question. Instead of using materialFound.material. I had to use materialFound.sharedMaterial. It was trying to find a key that wasn't available in the dictionary. sharedMaterial works perfectly for what I need. Thank you @NoDumbQuestion
private void OnTriggerEnter(Collider collision)
{
if (clips.Count == 0)
return;
AudioClip clip;
PhysicMaterial materialFound;
if (collision.sharedMaterial != null)
{
materialFound = collision.sharedMaterial;
if (clips.TryGetValue(materialFound, out clip))
{
PlayRequestedAudio(clip);
}
else
{
PlayDefaultAudio();
}
Debug.Log("Hit detected. attempting to play sound ==> " + materialFound.name.ToString());
}
}
private void PlayRequestedAudio(AudioClip audio)
{
source.PlayOneShot(audio);
}
private void PlayDefaultAudio()
{
if (defaultImpact == null)
return;
source.PlayOneShot(defaultImpact);
}
Answer by Zodiarc · Jul 05, 2018 at 06:47 AM
What I can imagine as the cause is that the key of the dictionary is a reference to an object. So even if the objects are equal, they're not the same and since the key stores the reference the dictionary of course can't find the reference to the material from the collider since it's different. Also comparing the references is faster than doing a deep inspection of the object (which isn't generic, so it would require a separate implementation for every existing class and possible combinations of them).
Instead using the material object as the key I'd use the material name. you should be able to access it by the public name variable.
Actually you can use the GetInstanceID method which returns an integer and is unique for each object.
public Dictionary<int, AudioClip> clips = new Dictionary<int, AudioClip>();
Physic$$anonymous$$aterial materialFound = collision.shared$$anonymous$$aterial;
AudioClip clip;
if (clips.TryGetValue(materialFound.GetInstanceID(), out clip))
If it's unique for each object it still won't work if I'm not mistaken since the Id in the dictionary wouldn't be the same as from the material in the collision. But I may oversee something.
It is, if it collides with the same shared material? that's the whole point of the dictionary
Your answer
![](https://koobas.hobune.stream/wayback/20220612170052im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Shooting myself 0 Answers
Falldamage has no effect on my HB 2 Answers
How do I create a Key and Values Dictionary array in C# 1 Answer