Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SendMessage doesn't launch function (HoloLens/Unity/C#)

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

    }
}
like image 885
Benjamin Naesen Avatar asked Aug 27 '26 20:08

Benjamin Naesen


1 Answers

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.

like image 154
Programmer Avatar answered Aug 30 '26 11:08

Programmer



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!