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. Voting for the Unity Awards are OPEN! We’re looking to celebrate creators across games, industry, film, and many more categories. Cast your vote now for all categories
    Dismiss Notice
  3. Dismiss Notice

Switching between scenes - a one way problem...

Discussion in 'Scripting' started by Gary_B, Jan 11, 2018.

  1. Gary_B

    Gary_B

    Joined:
    May 29, 2017
    Posts:
    21
    I'm trying to switch between 2 scenes using the voice command 'switch'. However it seems to be only going from my main scene (Scene1) to the secondary scene (Scene2). I thought it may have been something in the code, so I swapped the Scene1 & Scene2 around, but still it doesn't seem to be working.
    It happily goes from 1 to 2, but if I start in 2 it won't go to 1. I have tried both in the Hololens and in runtime in Unity and the same problem exists.

    Any help would be appretiated.

    The code is -

    Code (CSharp):
    1. using HoloToolkit.Unity.InputModule;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5. using UnityEngine.SceneManagement;
    6.  
    7. public class SwitchController : MonoBehaviour, ISpeechHandler {
    8.  
    9.     public void OnSpeechKeywordRecognized(SpeechEventData eventData)
    10.     {
    11.  
    12.         // Create a temporary reference to the current scene.
    13.         Scene currentScene = SceneManager.GetActiveScene();
    14.  
    15.         // Retrieve the name of this scene.
    16.         string sceneName = currentScene.name;
    17.  
    18.         if (sceneName == "Scene2")
    19.         {
    20.             SceneManager.LoadScene("Scene1");
    21.         }
    22.  
    23.         else if (sceneName == "Scene1")
    24.         {
    25.             SceneManager.LoadScene("Scene2");
    26.         }
    27.          
    28.    
    29.     }
    30. }

    The settings in both Scenes are -

     
  2. Gary_B

    Gary_B

    Joined:
    May 29, 2017
    Posts:
    21
    SOLVED

    I forgot to put the 'Input Manager' into Scene2....oops.


    Code works beautifully :)
     
    Last edited: Jan 11, 2018
  3. derdimi

    derdimi

    Joined:
    Mar 6, 2017
    Posts:
    33
    Did you try to disable the "Persistent Keywords" setting?

    Edit: I rebuild your example scenes and it's working fine for me. However, you should either use "Persistent Keywords" to keep your Speech Control GameObject alive between scenes or use separate Speech Controls for each scene but switch off "Persistent Keywords" in that case. And don't forget to add the Input Manager to both scenes.
     
    Last edited: Jan 11, 2018
    Gary_B likes this.
  4. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    is that script in both scenes?
     
  5. Gary_B

    Gary_B

    Joined:
    May 29, 2017
    Posts:
    21
    Yeah
     
  6. Gary_B

    Gary_B

    Joined:
    May 29, 2017
    Posts:
    21

    Thanks for the advice :)

    The issue was I'd forgotten to put the input manager in the 2nd scene. Works great!!
     
  7. kestler66

    kestler66

    Joined:
    Dec 28, 2022
    Posts:
    2
    Is there any way you can help me with this one, I would like to change scenes with voice command, I tried running the above example with the code but I am facing errors.
    Any help would be highly appreciated.