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 () {
}
}
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.