Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more..
    Dismiss Notice
  3. Dismiss Notice

Voice Recognition Start() problem and - Keep Random Number - Or Get Component

Discussion in 'Scripting' started by ashkanaral, Mar 6, 2020.

  1. ashkanaral

    ashkanaral

    Joined:
    Sep 3, 2018
    Posts:
    22
    I am using a key word recognizer script. The code I got from youtube shows the keywords in the dictionary to have everything in the start. So basically, adding to the dictionary of the keywords is in the start() menu. The word or phrase that is said is of course in the dictionary including the action which is the problem.
    I am trying to use a prefab instantiated to equal in if statement to equal the word said. So basically, when keyword "Hello" is said it should say that and check that prefab. If "World" is said it should not.

    First, since I used a random number for my prefab to spawn, I made a method named ARandomMethod() outside of Update(). The method variable is now in the prefab array[ARandomMethod]. The method is also in another method TestVoiceCommands() that checks to see if the random number from ARandomMethod() is equal to the correct element number starting 0 in inspector. This TestVoiceCommands() method is in the keywords dictionary action. The problem is that when I Debug.Log the ARandomMethod() it gives me a different number. I did it on the update with two Debug.Log before and after instantiate.

    However, I think I need to make the random number the same throughout the whole code. Also, the random code has to go to the start menu as well, but I have used a method to go to another method.

    What I am trying to say is how to make the random number variable the same throughout the whole code?

    Code (CSharp):
    1. // the voice commands work
    2. void Start() {
    3. keywords.Add("Hello", () =>
    4.         {
    5.                 TestVoiceCommands("Hello", 5);
    6.         });
    7. }
    8.   void Update() {
    9.   Instantiate(prefabs[ARandomMethod)]);
    10. }
    11.  
    12. public void TestVoiceCommands(string voiceSaid, int elemNum)
    13.     {
    14.         if (ARandomMethod() == elemNum)
    15.         {
    16.             //  ....
    17. }
    18. }
    19.    int ARandomMethod()
    20.      {
    21.          int ranNum = Random.Range(0, prefabs.Length);
    22.          return ranNum;
    23.      }

    Unless there is another way of coding to find the prefab checking instantiated in Update() to a set prefab.

    Code (CSharp):
    1. void start() {
    2.        keywords.Add("Hello", () =>
    3.         {
    4.          if(prefab[5] == ?? getComponent? // still prefab[5] has to be the one in Update()
    5. // though where it is randomized
    6.                 TestVoiceCommands("Hello", 5);
    7.         });
    8. }
     
    Last edited: Mar 7, 2020
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,769
  3. ashkanaral

    ashkanaral

    Joined:
    Sep 3, 2018
    Posts:
    22
    I read the article. It still does not sink in. What I am trying to do is use the random variable in Update() where it is instantiated. Then, there is a method where the variable of that same random number is used is placed in the Start() function as shown in the code I wrote above. When I put Debug.Log just a line with ARandomMethod() that has the random number. It gives me one number. This number changes each time I go into play mode. However, it never repeats as I am instantiating in a loop in Update().
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,769
    Update() runs every frame. Are you sure you want to Instantiate an object every frame?

    Instantiate will give you back a reference to what it Instantiates, which you can then save in a member variable and use in the future. Is that what you're looking for??
     
  5. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,769
    Looking closer at the code in the snippet above, it obviously doesn't compile given the mis-nesting of parentheses and square brackets.

    Typing and capitalization and punctuation is 100% critical in programming. You have to match your square braces with your parenthesis to start with. It may be helpful to back up a bit and look at some simple tutorials on understanding the code you are making. Trying random stuff out in a script is fun but it is unlikely to work and even less likely to give you what you are looking for.
     
  6. ashkanaral

    ashkanaral

    Joined:
    Sep 3, 2018
    Posts:
    22
    Thanks for reply. The code is snippets. Again, I am trying to make the instantiated prefab equal the voice command when said. So, I am thinking of using a random number that is inside the array of instantiated prefab to equal an element number (element number is from inspector). Hopefully, I explained it correctly.
     
  7. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,769
    By far the most complicated piece of tech here is the "voice recognition." Does that even work? I don't know what library you're using but I do know that under the best of circumstances, voice recognition is a crapshoot for video games. Did you make a simple test bed that listens to you, uses whatever library you're using there to figure out what you said, and display what it thinks you said? I assumed you had that working but if you don't, nothing else is worth touching until you do.
     
  8. ashkanaral

    ashkanaral

    Joined:
    Sep 3, 2018
    Posts:
    22
    The voice recognition is working. It works really good also on pretty bad accents. I am using windows platform. I just need to divide the voice commands between each commands to make a score, or they will make a score on whichever word you say in the dictionary.

    Also, I am not sure about this. Everything has to be in the Start() function for voice recognition? I do not know how? I have searched everywhere. Is there a variable or move keywords.Add I can move to Update() that would be nice.
     
  9. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,769
    Let's back up and take it from the top.

    If you already have a string coming back from the voice recognition, what exact part of looking a string up in a Dictionary are you having trouble with?

    Also, you say those are only snippets above, but I see
    start()
    is not capitalized, which means Unity will NOT be calling it, so if you rely on that code happening, it ain't happening.
     
  10. ashkanaral

    ashkanaral

    Joined:
    Sep 3, 2018
    Posts:
    22
    I was writing it in the forum sorry. It is not the problem, but I will edit in forum. The random number coming back to the dictionary where the action takes place is in the Start() function. So, I believe from the Debug.Log the random number stays always the same even after the Update() loops.

    This is before the start. I just want to show you what dictionary and how it is being used.
    The System.Action is the part I am trying to utilize.
    Code (CSharp):
    1.     KeywordRecognizer keywordRecognizer;
    2.     Dictionary<string, System.Action> keywords = new Dictionary<string, System.Action>();
    Basically, I think Start() will happen only one time for random numbers from loops even when you trace it back from another method or Update() function.

    Do you know any way I can call a prefab like GetComponent()?
     
  11. ashkanaral

    ashkanaral

    Joined:
    Sep 3, 2018
    Posts:
    22
    I found out that the dictionary keywords can be called in Update() such as this
    Code (CSharp):
    1.  keywords["Hello"]();
    Only problem is that I need to take out the system actions in the Start() function and put them in Update().

    So, how do I manipulate the keywords["Hello"]; and add system action in Update() function.

    This is code below is in Update(). The first if statement is giving me an error and wrong but is what I need. It has system action testVoiceCommand method. I do not know how to write this system action while calling keywords["Hello"]; The second if statement is just for show to explain. This if statement that does not have any system action just uses the Start() function because it only calls keywords["World"]();

    You cannot remove the keywords.Add from the Start() and put it in Update() it will give error

    Code (CSharp):
    1. if (ranPreFabNum == 5)
    2.             {
    3.                 keywords["Hello"]() =>
    4.         {
    5.                     testVoiceCommand("Hello", 100);
    6.                 });
    7.  
    8.             }
    9.             else if (ranPreFabNum == 4)
    10.             {
    11.                 keywords["World"]();
    12.             }