- Home /
 
Open scene by typing a word
Hello everyone, i am kinda trapped in a script here. I wanted to make an easter egg thing in my game. Something like if the player types TILT in sequence ( "T" then "I" then "L" then "T") the game would change to another scene. I have a script here that a friend helped me to do, and it does what i want. At least the most... It loads a specific scene when i type TILT, but ONLY in something like a command chat. I wanted to remove that feature, and add just type. You know, nothing like click on the command chat and then type TILT, i wanted the player just to type TILT whenever he wanted in whatever place.
Here's the script: (It's a little complicated)
 using UnityEngine;
 using UnityEngine.SceneManagement;
 using UnityEngine.UI;
 using System.Collections;
 
 public class LoadLevelCheatCode : MonoBehaviour
 {
 
     public string cheatcode = "tilt";
 
     public void SubmitCode()
     {
 
         if (!GetComponent<InputField>()) {
             Debug.LogError("Code wasn't typed correctly");
             return;
         } 
         string code = GetComponent<InputField>().text;
         //This will try to see if you typed in "tilt" in game. If you do (and you press enter or exit the input field),
         //it will try to load a level called cheatcode, which is a variable
         //If it doesn't find a scene called cheatcode, we should see an error
 
 
         if (code.Equals("tilt")) SceneManager.LoadScene(cheatcode);
     }
 }
 
              Answer by Jessespike · Sep 09, 2016 at 08:52 PM
 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 public class InputKeywords : MonoBehaviour {
 
     const int MaxInputHistory = 8;
     public List<string> inputHistory = new List<string>();
 
     // Update is called once per frame
     void Update () {
         
         if (!string.IsNullOrEmpty(Input.inputString))
         {
             inputHistory.Insert(0, Input.inputString);
             while(inputHistory.Count > MaxInputHistory)
             {
                 inputHistory.RemoveAt(inputHistory.Count-1);
             }
         }
 
         if (CheckForKeyword("tilt"))
         {
             Debug.Log("CheckForKeyword: tilt detected");
             // TODO implmentation
             inputHistory.Clear();
         }
     }
 
     bool CheckForKeyword(string keyword)
     { 
         if (inputHistory.Count >= keyword.Length)
         {
              for (int i = 0; i < keyword.Length; i++)
              {
                 if (keyword[keyword.Length-1-i].ToString() != inputHistory[i])
                 {
                     // input does not match keyword
                     return false;
                 }
             }
             // input and keyword match has been found
             return true;
         }
 
         // not enough input to do comparison
         return false;
     }
 }
 
              Answer by doublemax · Sep 09, 2016 at 07:47 PM
 private string current_word;
 private string cheat_code = "tilt";
 
 void Update()
 {
   CheckCheatCode();
 }
 
 private void CheckCheatCode()
 {
   string s = Input.inputString;
   if ( s != "" )
   {
     current_word += s;
     if (current_word == cheat_code)
     {
       Debug.Log("Cheat code detected");
       current_word = "";
     }
     else
     {
       if (!cheat_code.StartsWith(current_word))
       {
         current_word = "";
       }
     }
   }
 }
 
              Your answer
 
             Follow this Question
Related Questions
How do I load a new scene based on current scene? 3 Answers
Change Material in other Scene by Scripting 0 Answers
Scene Change Collision 2 Answers
Load scene when colliding on GameObject 1 Answer
Click Object to change scene 2 Answers