Search Unity

Registering part of a string?

Discussion in 'Scripting' started by GamesOnAcid, Feb 12, 2016.

  1. GamesOnAcid

    GamesOnAcid

    Joined:
    Jan 11, 2016
    Posts:
    283
    Quick question, I was setting up a keycode to unlock a door, and I decided a pretty efficient way to do this would be:
    Set functions for the 1-9 keys.
    Every key prints an individual number to a string.
    If that string = the password, unlock the door.

    Is this even possible? If so, let me know how to do it, or point me to a tutorial. Any help would be appreciated, thanks guys!
     
  2. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,531
    It's just string appending...

    Code (csharp):
    1.  
    2. private string _input;
    3.  
    4. void Key9Pressed()
    5. {
    6.     _input += "9";
    7. }
    8.  
    Is there something specific you need help with?
     
  3. GamesOnAcid

    GamesOnAcid

    Joined:
    Jan 11, 2016
    Posts:
    283
    Was just going to update this, I had a code error and was looking for a new outlook. Shouldn't have given up so quickly, appending strings will work just fine. Thank you
     
  4. Josenifftodd

    Josenifftodd

    Joined:
    Nov 29, 2013
    Posts:
    158
    Code (CSharp):
    1. if (Input.GetKeyDown(keycode.9) {
    2. // do something here
    3. }
    4. if (Input.GetKeyDown(keycode.8) {
    5. // do something here
    6. }
    7. if (Input.GetKeyDown(keycode.7) {
    8. // do something here
    9.  }
    Id do it this way... ;)
     
    GamesOnAcid likes this.