Problem with cheatcode

I am trying to make a script that when the player types “TILT” in sequency, the game changes to a new scene. BUT i am having a lot of problems, as usual T.T

I found this script in the forum and i update it, but it doesn’t work because of some problems.

using UnityEngine;
using System.Collections;

public class eastereggscene : MonoBehaviour {

    private string[] cheatCode;
    private int index;

    void Start()
    {
        // Code is "tilt", user needs to input this in the right order
        cheatCode = new string[] { "t", "i", "l", "t"  };
        index = 0;
    }

    void Update()
    {
        // Check if any key is pressed
        if (Input.anyKeyDown)
        {
            // Check if the next key in the code is pressed
            if (Input.GetKeyDown(cheatCode[index]))
            {
                // Add 1 to index to check the next key in the code
                index++
           { 
            // Wrong key entered, we reset code typing
            else {
                index = 0;
            }
        }

        // If index reaches the length of the cheatCode string, 
        // the entire code was correctly entered
        if (index == cheatCode.Length)

           }
            else
            {
                //load level
                Application.LoadLevel(1);
            }
        }

The following errors appear:

Assets/eastereggscene.cs(26.12): Error CS1525: Unexpected Symbol ‘{’
Assets/eastereggscene.cs(37.12): Error CS1525: Unexpected Symbol ‘}’

If i remove those keys, (i don’t know if this is the correct name, english is not my mother language),
i get these errors:

Assets/eastereggscene.cs(28.16): Error CS1525: Unexpected Symbol ‘else’
Assets/eastereggscene.cs(38,16): Error CS1525: Unexpected Symbol ‘else’

What is wrong?

You misused curly brackets At line 26 change '{' to '}' and at line 36 add '{'

3 Answers

3

you missed out a few curly brackets and a semi-colon. Here’s the code

 using UnityEngine;
 using System.Collections;
  
  public class eastereggscene : MonoBehaviour 
  {
      private string[] cheatCode;
      private int index;
  
      void Start()
      {
          // Code is "tilt", user needs to input this in the right order
          cheatCode = new string[] { "t", "i", "l", "t"  };
          index = 0;
      }
  
      void Update()
      {
          // Check if any key is pressed
          if (Input.anyKeyDown)
          {
              // Check if the next key in the code is pressed
              if (Input.GetKeyDown(cheatCode[index]))
              {
                  index++;
              }
              // Wrong key entered, we reset code typing
              else 
              {
                  index = 0;
              }
          }
  
          // If index reaches the length of the cheatCode string, 
          // the entire code was correctly entered
          if (index == cheatCode.Length)
          {
              Application.LoadLevel(1);
          }
      }
  }

Thanks! Everyone's code was incorrect too but you fixed it! =)

You do a few syntax error there, let me fix this :slight_smile:

 using UnityEngine;
 using System.Collections;
 
 public class eastereggscene : MonoBehaviour {
 
     private string[] cheatCode;
     private int index;
 
     void Start()
     {
         // Code is "tilt", user needs to input this in the right order
         cheatCode = new string[] { "t", "i", "l", "t"  };
         index = 0;
     }
 
     void Update()
     {
         // Check if any key is pressed
         if (Input.anyKeyDown)
         {
             // Check if the next key in the code is pressed
             if (Input.GetKeyDown(cheatCode[index]))
             {
                 // Add 1 to index to check the next key in the code
                 index++
            }
             // Wrong key entered, we reset code typing
             else {
                 index = 0;
             }
         }
 
         // If index reaches the length of the cheatCode string, 
         // the entire code was correctly entered
         if (index == cheatCode.Length)
                 Application.LoadLevel(1);
         }

}

There it should be good, but the indentation is not perfect, use Ctrl + K, D to format it properly in VS(I don’t know the touches for MonoDevelop).

Hope this do it for you, FireCube

PS: sorry for the last } not into the code block

also, try to give a look at the c sharp naming convention, it will be easier to other to look at your code

It still gives me the same error, and Ctrl + K + D is not a command in VS 2015 =C

This is correct but the file should end like this: if (index == cheatCode.Length) { Application.LoadLevel (1); } } } } :) Add add a semi-colon after index++

Hello

Zitoox

I wanted to help too… your curly braces were a total mess, everytime you open anything that has a closing match, you must be sure to close it

  • " " Double quotes
  • ( ) Parenthesis
  • Square brackets
  • { } Curly braces
  • ’ ’ Single quote
  • < > Angle brackets

These are the basics in programming

if (conditional operation inside parenthesis)
			 { //Open curly brace
				 conditional body inside curly braces
			 } //Close curly brace

Here’s your code

using UnityEngine;
using System.Collections;
 
 public class eastereggscene : MonoBehaviour 
 {
	 private string[] cheatCode;
     private int index;
 
     void Start()
     {
         // Code is "tilt", user needs to input this in the right order
         cheatCode = new string[] { "t", "i", "l", "t"  };
         index = 0;
     }
 
     void Update()
	 {
		 // Check if any key is pressed
         if (Input.anyKeyDown)
         {
             // Check if the next key in the code is pressed
             if (Input.GetKeyDown(cheatCode[index]))
			 {
				 index++
			 }
             // Wrong key entered, we reset code typing
             else 
			 {
                 index = 0;
             }
         }
 
         // If index reaches the length of the cheatCode string, 
         // the entire code was correctly entered
         if (index == cheatCode.Length)
		 {
			 Application.LoadLevel(1);
		 }
	 }
 }

Hope it helps

Cheers

It still gives me the same error. but different =/ Assets/eastereggscene.cs(25,15): Error CS1525: Unexpected Symbol '}'

There is a missing ";" after index++ in line 24.

You are right, thanks! Updated the answer.