- Home /
Question by
its_ya_studio · Feb 25, 2021 at 09:27 AM ·
actions
sending multiple actions to the same void
i'm using a voice recognizer for some spells in a game and i try to add a new command for every element in the list elements but wen the voice recognizer recognizes a elements name it gives the error :
KeyNotFoundException: The given key was not present in the dictionary.
System.Collections.Generic.Dictionary`2[TKey,TValue].get_Item (TKey key) (at <9577ac7a62ef43179789031239ba8798>:0)
magicspels.recognized (UnityEngine.Windows.Speech.PhraseRecognizedEventArgs speech) (at Assets/scripts/magicspels.cs:107)
UnityEngine.Windows.Speech.PhraseRecognizer.InvokePhraseRecognizedEvent (System.String text, UnityEngine.Windows.Speech.ConfidenceLevel confidence, UnityEngine.Windows.Speech.SemanticMeaning[] semanticMeanings, System.Int64 phraseStartFileTime, System.Int64 phraseDurationTicks) (at :0)
how can i fix his ? it redirects all the elements to the same void elementscall()
using System.Collections;
using System;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using UnityEngine.Windows.Speech;
using System.Reflection;
public class magicspels : MonoBehaviour
{
private KeywordRecognizer keywordRecognizer;
private Dictionary<string, Action> actions = new Dictionary<string, Action>();
private bool called;
private bool generate;
private string reqword;
private GameObject Generated;
public Microphone mic;
public Transform instatiatePos;
public float yoffset;
public GameObject genPrefab;
public elements[] elementsList;
public shapes[] shapeList;
float xpos;
float ypos;
float zpos;
private void Start()
{
actions.Add("system call", systemCall);
actions.Add("generate", Generate);
for(int i = 0; i < elementsList.Length; i++)
{
actions.Add(elementsList[i].name + " element",Elementcall);
Debug.Log("added" + elementsList[i].name);
}
for (int i = 0; i < elementsList.Length; i++)
{
// actions.Add(elementsList[i].name, Shapecall);
}
keywordRecognizer = new KeywordRecognizer(actions.Keys.ToArray());
keywordRecognizer.OnPhraseRecognized += recognized;
keywordRecognizer.Start();
for(int i = 0; i < Microphone.devices.Length; i++)
{
Debug.Log(Microphone.devices[i]);
}
xpos = transform.position.x;
ypos = transform.position.y;
zpos = transform.position.z;
}
private void Update()
{
xpos = (transform.position.x + xpos * 10) / 11;
ypos = (transform.position.y + ypos * 10) / 11;
zpos = (transform.position.z + zpos * 10) / 11;
Vector3 realpos = new Vector3(xpos, ypos + yoffset, zpos);
instatiatePos.position = realpos;
}
private void recognized(PhraseRecognizedEventArgs speech)
{
Debug.Log(speech.text);
reqword = speech.text;
actions[speech.text].Invoke();
}
void systemCall()
{
called = true;
}
void Generate()
{
if (called)
{
generate = true;
Debug.Log("generated prefab");
Generated = Instantiate(genPrefab,instatiatePos.position,instatiatePos.rotation);
}
}
void Elementcall()
{
if (called)
{
Debug.Log("called");
if (generate)
{
Debug.Log("generated");
for (int i = 0; i <= elementsList.Length - 1; i ++)
{
Debug.Log("checking");
if (elementsList[i].name == reqword)
{
Debug.Log("check worked");
Debug.Log("worked " + reqword);
Generated.GetComponent<MeshRenderer>().material = elementsList[i].material;
}
}
}
}
}
[System.Serializable]
public struct elements
{
public string name;
public Material material;
public GameObject height;
}
[System.Serializable]
public struct shapes
{
public string name;
public Mesh shape;
}
}
Comment
can you paste the full code? it says key not found, so the error is when you try to access the dictionary not when you initialize it
Best Answer
Answer by xxmariofer · Feb 25, 2021 at 09:45 AM
Most likely you need to make a comprobation test this code
private void recognized(PhraseRecognizedEventArgs speech)
{
if(action.ContainsKey(speech.text))
{
Debug.Log(speech.text);
reqword = speech.text;
actions[speech.text].Invoke();
}
else
{
Debug.Log("Your dictionary doesnt contain " + speech.text);
}
}
@ xxmariorfer ok it works now thank you so much for the quick respond
Your answer
