Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
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
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image xxmariofer · Feb 25, 2021 at 09:30 AM 0
Share

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0
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); 
      }
  }
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image its_ya_studio · Feb 25, 2021 at 09:54 AM 0
Share

@ xxmariorfer ok it works now thank you so much for the quick respond

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

113 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Score Workflow 1 Answer

What is Unityevent and how do they relate to Unityaction? 1 Answer

Whats the most efficient/elegant way to code custom actions for same prefab 1 Answer

assign keyboard buttons 1 Answer

How to add a persistent method with a single argument to a UnityEvent? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges