Search Unity

Help with scripting keypad/keylock

Discussion in 'Scripting' started by tonymx, Dec 10, 2013.

  1. tonymx

    tonymx

    Joined:
    Oct 18, 2012
    Posts:
    11
    Question, I'm trying to script where you press specific buttons with mouse to change the scene. How do I do this?

    For example, let's say you are trying to script an electronic lock? The one that has numbers. If you press buttons "1,5,2,7" on the keypad buttons it will change the scene.
     
  2. BlackMantis

    BlackMantis

    Joined:
    Feb 7, 2010
    Posts:
    1,475
    Look in the documentation under GUI Buttons, and GUI TextArea. Once you have the ui setup its just a matter of using conditional statements to see if you have typed anything that matches a preset code for the number pad.

    Are you talking about something like this image below.

    $000.png
     
    Last edited: Dec 10, 2013
  3. tonymx

    tonymx

    Joined:
    Oct 18, 2012
    Posts:
    11
    Sorry, I'm a noob to scripting. I have no idea where to start...
     
  4. BlackMantis

    BlackMantis

    Joined:
    Feb 7, 2010
    Posts:
    1,475
    I think that there is a small GUI Tutorial available in the manual, but the real goodies / examples are located in the Help / Scripting References / Runtime Classes / Gui.
     
  5. tonymx

    tonymx

    Joined:
    Oct 18, 2012
    Posts:
    11
    Is it possible to script without using GUI at all?

    I already have gameobjects for each buttons and all on the scene.
     
  6. BlackMantis

    BlackMantis

    Joined:
    Feb 7, 2010
    Posts:
    1,475
    Well - yes, lol. To use a model, look into Input (ie) below. Make sure you have a collider on your buttons, then add a script to each 3D model button.

    Code (csharp):
    1.  
    2. if((Input.GetMouseButtonUp(0)) {
    3.   // Wash my car - lol
    4. }
    5.  
    I would suggest creating a main script on the parent of these buttons, then use an array or an array list to add the data coming from the buttons. For this you will need to communicate with the main script via GetComponent or static variable. Ofc there are many ways to skin a cat so you may be thinking of a whole different approach. :)
     
  7. tonymx

    tonymx

    Joined:
    Oct 18, 2012
    Posts:
    11
    I only need part where each buttons are recognized one after another when they are pressed.

    How would you script where, lets say if you press 1,2,3,4 you change level?

    I have each variables for those 4 buttons


    var button1 : GameObject;
    var button2 : GameObject;
    var button3 : GameObject;
    var button4 : GameObject;

    function OnMouseDown () {



    }
     
  8. BlackMantis

    BlackMantis

    Joined:
    Feb 7, 2010
    Posts:
    1,475
    (Tip) if you use something like MouseUp then you don't have to worry about unwanted extra mouse clicks or constant MouseDown.

    One way is to give each block a var that can be assigned in the Inspector, then ask something like this

    Code (csharp):
    1.  
    2. if(idVar=="Zero") {
    3.   keyPadCode = kayPadCode + 0; //
    4. }
    5. else if(idVar==One) {
    6.   keyPadCode = kayPadCode + 1; //
    7. }
    8.  
    9. Or you can simply just add from the idVar
    10.  
    11. keyPadCode = kayPadCode + idVar; //
    12.  
     
  9. tonymx

    tonymx

    Joined:
    Oct 18, 2012
    Posts:
    11
    Ugh, sorry my brain is fried for staying up too late to figure this out.

    I'm totally lost...

    Here's what it looks like. Each buttons are all separate objects on the scene.


    $wnjs.png
     
    Last edited: Dec 10, 2013
  10. BlackMantis

    BlackMantis

    Joined:
    Feb 7, 2010
    Posts:
    1,475
    Ok I'll hook you with some basic scripts. Make sure that your buttons have colliders and add this script. Also make sure your buttons have the same parent obj.

    Code (csharp):
    1.  
    2. var myButtonID : int; // Assign the ID num in the Inspector
    3.  
    4. var mainScript : MonoBehaviour;
    5. ///=======================================================///
    6. function Start() {
    7.     //------------//
    8.     if(transform.parent.GetComponent(PadScript)) {
    9.       //------------//
    10.       mainScript = transform.parent.GetComponent(PadScript);
    11.       //------------//
    12.     }
    13.     //------------//
    14. }
    15. ///=======================================================================///
    16. function Update() {
    17.     //------------//
    18.     if(Input.GetMouseButtonUp(0)) {
    19.       //------------//
    20.       if(mainScript) {
    21.         //------------//
    22.         mainScript.keyPadCode = mainScript.keyPadCode + myButtonID;
    23.         //------------//
    24.         Debug.Log(name + " - mainScript.keyPadCode ("+mainScript.keyPadCode+")");
    25.         //------------//
    26.       }  
    27.       //------------//
    28.     }
    29.     //------------//
    30. }
    31. ///=======================================================================///
    32.  
    And now for the parent script

    Code (csharp):
    1.  
    2. public var keyPadCode : String;
    3.  
    4. public var checkKeyCode : boolean;
    5. ///======================================================///
    6. function Update() {
    7.     //------------//
    8.     ///------// This bool can be caled from other scripts or used in the Inspector
    9.     //------------//
    10.     if(checkKeyCode) {
    11.       checkKeyCode = false; // Stop our condition so we only call the function once
    12.       CheckAndExecute( keyPadCode ); // Pass the keyCode
    13.     }
    14.     //------------//
    15. }
    16. ///======================================================///
    17. function CheckAndExecute( kCode : String ) {
    18.   //------------//
    19.   if(kCode==1111) {
    20.     //------------//
    21.     ///------// Your code for the event
    22.     //------------//
    23.     Debug.Log(name + " - We have a match with code ("+kCode+")");
    24.     //------------//
    25.     keyPadCode = ""; // Clear the code
    26.     //------------//
    27.   }
    28.   else if(kCode==2222) {
    29.     //------------//
    30.     ///------// Your code for the event
    31.     //------------//
    32.     Debug.Log(name + " - We have a match with code ("+kCode+")");
    33.     //------------//
    34.     keyPadCode = ""; // Clear the code
    35.     //------------//
    36.   }
    37.   else {
    38.     //------------//
    39.     Debug.Log(name + " - We do not have a match ("+kCode+")");
    40.     //------------//
    41.     keyPadCode = ""; // Clear the code
    42.     //------------//
    43.   }
    44.   //------------//
    45. }
    46. ///======================================================///
    47.  
     
    Last edited: Dec 10, 2013