• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by purpleduck · Feb 23, 2011 at 03:16 AM · type

Loading easter egg on typing a secret keyword.

I would like an easter egg to be opened during my game if a certain word is typed, that is, when the user types "cola" (example) on the keyboard. None of these keys would be used for anything else but the easter eggs.

I wrote the following script, and it does load the easter egg scene when I type "cola", but the user should type the letters in the exact order, and any mistype should make the block start all over - this doesn't happen yet. I'm not sure what conditions to add to make this happen.

I have tried with conditions for each letter like

if (Input.anyKey && ! Input.GetKey("u")) cola= 0

to make the cola variable restart if the user presses any key EXCEPT the "u"...but I didn't see this working. I am very new to scripting of course. Does the above condition equal "if you press anykey EXCEPT "u""?

Here's the script:

private var cola = 0;

function Update () {

// cola count

if (Input.GetKey("c") && cola < 1) { cola = 1; }

if (Input.GetKey("o") && cola < 2 && cola >= 1) { cola = 2; }

if (Input.GetKey("l") && cola < 3 && cola >= 2) { cola = 3; }

if (Input.GetKey("o") && cola < 4 && cola >= 3) { cola = 4; Application.LoadLevel("EasterEgg"); }

}

Of course, if there is a better way to do thing using maybe a string variable, and giving me the possibility to load other easter eggs when typing different words which might include the very same letters, please let me know. But I would still like to know if:

if (Input.anyKey && ! Input.GetKey("u")) cola= 0

tells what I want it to tell.

Thanks!

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Minthos · Feb 24, 2011 at 11:02 PM 1
Share

cola < 4 && cola >= 3

is equivalent to:

cola == 3

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by karl_ · Feb 25, 2011 at 12:20 AM

Instead of using Input.GetKey, I went with the Input.inputString. This script will read all the keyboard input and load the easter egg level on the condition that the secret word is typed.

Edit : Now allows for multiple secret words.

var easterEggWord : String[] ; // Make sure the corresponding keyword and lvlToLoad match var levelToLoad : String[]; private var easterEgg : String = "";

function Update () { for (var curLetter : char in Input.inputString) { easterEgg += curLetter; checkForSecretWord(); Debug.Log("Current string " + easterEgg); } }

function checkForSecretWord() { for(iterator = 0; iterator < easterEggWord.length; iterator++) { if(easterEgg.Contains(easterEggWord[iterator]) ) Application.LoadLevel(levelToLoad[iterator]); } }

Comment
Add comment · Show 6 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image · Feb 25, 2011 at 12:27 AM 1
Share

The problem with this solution is that if the player enters "acola" then it won't match correctly, because the string will be cleared when it reaches "acol", and the player has no way of knowing the current length of the inputString.

avatar image karl_ · Feb 25, 2011 at 01:45 AM 0
Share

Fixed it. Bit more robust and accounts for the error you pointed out.

avatar image purpleduck · Feb 25, 2011 at 06:37 AM 0
Share

Brilliant, looks very solid. I'll validate your answer, but first, can you tell me why this condition would not work?

if (Input.any$$anonymous$$ey && !Input.Get$$anonymous$$ey("o")) cola = 0

I just don't see a reason. It does not tell Unity what I hoped it would tell. It does NOT tell: if any key EXCEPT "o" gets pressed. It gets executed everyframe, ins$$anonymous$$d!

I also tried inserting such condition inside a for(Input.any$$anonymous$$ey) and guess what? It would crash Unity.

avatar image karl_ · Feb 25, 2011 at 10:11 AM 0
Share

Input.Get$$anonymous$$ey is called every frame - http://unity3d.com/support/documentation/ScriptReference/Input-any$$anonymous$$ey.html ,as is Input.any$$anonymous$$ey. Hence, placing this in a for loop will inevitably crash Unity as it will be continuously cycling through the statement. I'd read up on the docs regarding Input to get a more in depth understanding of how exactly the Input.any$$anonymous$$ey and Get$$anonymous$$ey functions work.

avatar image purpleduck · Feb 25, 2011 at 06:37 PM 0
Share

Ok, so...(Input.any$$anonymous$$ey && !Input.Get$$anonymous$$ey("o")) wouldn't work for this reason? It doesn't crash because of this unless I put it inside a loop. I'm wondering with the !Input.Get$$anonymous$$ey("o") boolean just doesn't work.

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

No one has followed this question yet.

Related Questions

Boo help: method not defined 4 Answers

enable / disable a script without dynamic typing 2 Answers

"Type" type 1 Answer

How to set animations to loop? 1 Answer

Easy? Convert System.Type to UnityEngine.Component 3 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges