SendMessage does not launch function (HoloLens / Unity / C #)
Purpose: Transition from one scene to another using auditing.
Problem: Launching the app in the HoloLens emulator opens the first scene. By saying Next Step, HoloLens recognizes the sentence, but sendMessage doesn't open the function OnNextStep()
.
Thanks for the 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 () {
}
}
source to share
Unity SendMessage
function is case sensitive when it comes to calling functions.
Your function name OnNextstep
, but you call OnNextstep
:
SendMessage("OnNextStep", SendMessageOptions.DontRequireReceiver);
Notice the capitalized and uncapitalized "S" . Fix it and your problem should be fixed if you have any other hidden issues.
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 for this.
source to share