3

Goal: Changing from one scene to another using auditive controls.

Problem: When launching the application in the HoloLens Emulator, the first scene opens. When saying "Next Step", the HoloLens does recognize the sentence, but the sendMessage doesn't open the OnNextStep() function.

Thanks for trying to help! :)

using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using UnityEngine.Windows.Speech;
using System.Diagnostics;
using UnityEngine.SceneManagement;

public class KeywordManager : MonoBehaviour {

    KeywordRecognizer keywordRecognizer = null;
    Dictionary<string, System.Action> keywords = new Dictionary<string, System.Action>();

    // Use this for initialization
    void Start () {
        keywords.Add("Next Step", () =>
        {
            SendMessage("OnNextStep", SendMessageOptions.DontRequireReceiver);
        });

        // Tell the KeywordRecognizer about our keywords.
        keywordRecognizer = new KeywordRecognizer(keywords.Keys.ToArray());

        // Register a callback for the KeywordRecognizer and start recognizing!
        keywordRecognizer.OnPhraseRecognized += KeywordRecognizer_OnPhraseRecognized;
        keywordRecognizer.Start();
    }

    private void KeywordRecognizer_OnPhraseRecognized(PhraseRecognizedEventArgs args)
    {
        System.Action keywordAction;
        if(keywords.TryGetValue(args.text, out keywordAction))
        {
            keywordAction.Invoke();
        }
    }

    void OnNextstep()
    {
        UnityEngine.Debug.Log(this);
        SceneManager.LoadScene("FirstStepScene");
    }

    // Update is called once per frame
    void Update () {

    }
}
Benjamin Naesen
  • 174
  • 1
  • 10
  • 1
    1.Post your code not screenshot. 2.Use `UnityEngine.Debug.Log` not `System.Diagnostics.Debug.WriteLine`. With Debug.Log you can now tell us which function is not working properly. tell us if `OnNextstep` is being called at-all. – Programmer Jun 01 '17 at 12:16
  • Code has been added. OnNextStep is NOT called. – Benjamin Naesen Jun 01 '17 at 12:20
  • 1
    You also don't need `SendMessage` here. You can just invoke the method directly: `keywords.Add("Next Step", () => { OnNextStep(); });` Whiiich would have the side effect of pointing out that the method name didn't match exactly. ;) – Draco18s no longer trusts SE Jun 01 '17 at 15:10

1 Answers1

4

Unity's SendMessage function is case sensitive when it comes to calling functions.

Your function name is OnNextstep but you are calling OnNextStep:

SendMessage("OnNextStep", SendMessageOptions.DontRequireReceiver);

Notice the capitalized and non capitalized "S". Fix that and your problem should be fixed assuming there is other hidden problems.

Note:

Avoid using SendMessage in Unity. If you want to call a function from another script, use GameObject.Find to find the GameObject then GetComponent to get that script then call its function. You can also use events and delegates to do this.

lysergic-acid
  • 19,570
  • 21
  • 109
  • 218
Programmer
  • 121,791
  • 22
  • 236
  • 328
  • 1
    Thank you very much for the answer and your advice. I have roughly 4 weeks to create HoloLens software to visualize a repair process and we never studied anything Unity nor HoloLens nor 3D modelling. So your help is very much appreciated. I'll put you on the credits list of my project :D – Benjamin Naesen Jun 01 '17 at 12:28
  • 1
    You are welcome. If this is a school project, don't wait for the teacher, go online and start learning. Many information on Unity' [website](https://unity3d.com/learn/tutorials). – Programmer Jun 01 '17 at 12:31