Search Unity

  1. Unity 2017.2 beta is now available for download.
    Dismiss Notice
  2. Unity 2017.1 is now released.
    Dismiss Notice
  3. Introducing the Unity Essentials Packs! Find out more.
    Dismiss Notice
  4. Check out all the fixes for 5.6 on the patch releases page.
    Dismiss Notice
  5. Help us improve the editor usability and artist workflows. Join our discussion to provide your feedback.
    Dismiss Notice

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. Black-Mantis

    Black-Mantis

    Joined:
    Feb 7, 2010
    Posts:
    1,457
    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. Black-Mantis

    Black-Mantis

    Joined:
    Feb 7, 2010
    Posts:
    1,457
    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. Black-Mantis

    Black-Mantis

    Joined:
    Feb 7, 2010
    Posts:
    1,457
    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.   // Wash my car - lol
    3. }
    4.  
    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. Black-Mantis

    Black-Mantis

    Joined:
    Feb 7, 2010
    Posts:
    1,457
    (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. Black-Mantis

    Black-Mantis

    Joined:
    Feb 7, 2010
    Posts:
    1,457
    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