Search Unity

button getkeys

Discussion in 'Scripting' started by Rbert, Sep 16, 2014.

Thread Status:
Not open for further replies.
  1. Rbert

    Rbert

    Joined:
    Jul 13, 2014
    Posts:
    28
    can you help me to code this in getkey..because i want to use keybord , my game is like a 4 pics 1 word..


    Code (CSharp):
    1. public class play : MonoBehaviour {
    2.     public Texture[] icon;
    3.     public string answer;
    4.     public string x = "";
    5.     public ArrayList Let = new ArrayList ();
    6.     public ArrayList letters = new ArrayList ();
    7.     public string l = "QWERTYUIOPASDFGHJKLZXCVBNM";
    8.     public string c = "";
    9.     public string[] files;
    10.     public ArrayList exampleFiles;
    11.     public string whatToGuess;
    12.     public ArrayList isAvailable;
    13.     public ArrayList toBeRemoved;
    14.     //file
    15.     public string word;
    16.     public string tagalog;
    17.     public string english;
    18.     public bool hasRun = false;
    19.     public bool[] completed = new bool[5];
    20.     public bool fin;
    21.     public bool nahulaan=false;
    22.     void OnGUI () {
    23.        
    24.         try
    25.         {
    26.             if(GUI.Button(new Rect(500,420,50,20), "Next")) {
    27.                 Application.LoadLevel(0);
    28.             }
    29.             if (GUI.Button (new Rect (100,50,130,130),new GUIContent (icon[0])))
    30.             {
    31.                
    32.             }
    33.             if (GUI.Button (new Rect (250,50,130,130),new GUIContent (icon[1])))
    34.             {
    35.                
    36.             }
    37.             if (GUI.Button (new Rect (400,50,130,130),new GUIContent (icon[2])))
    38.             {
    39.                
    40.             }
    41.             if (GUI.Button (new Rect (550,50,130,130),new GUIContent (icon[3])))
    42.             {
    43.                
    44.             }
    45.            
    46.            
    47.             if(fin==false)
    48.             {
    49.                 for (int i=0; i<6; i++) {
    50.                     if (isAvailable [i] == "1") {
    51.                         if ((GUI.Button (new Rect (300 + ((i % 6) * 40), 330, 30, 30), "" + letters [i])) && (Let.Count < answer.Length)) {
    52.                             Let.Add (letters [i]);
    53.                             isAvailable [i] = 0;
    54.                             toBeRemoved.Add (i);
    55.                         }
    56.                     }
    57.                     else
    58.                     {
    59.                         GUI.Box (new Rect (300 + ((i % 6) * 40), 330, 30, 30), "");
    60.                     }
    61.                 }
    62.                
    63.                 for (int i=6; i<12; i++) {
    64.                     if (isAvailable [i] == "1") {
    65.                         if ((GUI.Button (new Rect (300 + ((i % 6) * 40), 370, 30, 30), "" + letters [i])) && (Let.Count < answer.Length)) {
    66.                             Let.Add (letters [i]);
    67.                             isAvailable [i] = 0;
    68.                             toBeRemoved.Add (i);
    69.                         }
    70.                     }
    71.                     else
    72.                     {
    73.                         GUI.Box (new Rect (300 + ((i % 6) * 40), 370, 30, 30), "");
    74.                     }
    75.                    
    76.                 }
    77.                 for (int i=0; i<answer.Length; i++) {
    78.                     if (i < 6) {
    79.                         GUI.Box (new Rect (300 + ((i % 6) * 40), 280, 30, 30), "");
    80.                     } else if ((i >= 6) && (i < 12)) {
    81.                         GUI.Box (new Rect (300 + ((i % 6) * 40), 310, 30, 30), "");
    82.                     }
    83.                 }
    84.                 if ((GUI.Button (new Rect (540, 280, 30, 30), "<"))&&(nahulaan==false)) {
    85.                     if (Let.Count > 0) {
    86.                         Let.RemoveAt (Let.Count - 1);
    87.                         isAvailable [(int)toBeRemoved [toBeRemoved.Count - 1]] = "1";
    88.                         toBeRemoved.RemoveAt (toBeRemoved.Count - 1);
    89.                     }                  
    90.                 }
    91.                
    92.                 x = "";
    93.                 for (int i=0; i<Let.Count; i++) {
    94.                     if (i < 6) {
    95.                         GUI.Box (new Rect (300 + ((i % 6) * 40), 280, 30, 30), "" + Let [i]);
    96.                     } else if ((i >= 6) && (i < 12)) {
    97.                         GUI.Box (new Rect (300 + ((i % 6) * 40), 310, 30, 30), "" + Let [i]);
    98.                     }
    99.                    
    100.                     x = x + Let [i];
    101.                 }
    102.                 if ((x == answer)&&(nahulaan==false)) {
    103.                    
    104.                     string _strDBName = "URI=file:"+Application.dataPath+"/dbfile";
    105.                    
    106.                     IDbConnection _connection = new SqliteConnection(_strDBName);
    107.                     IDbCommand _command = _connection .CreateCommand();
    108.                     string sql;
    109.                    
    110.                     _connection.Open();
    111.                    
    112.                     sql = "CREATE TABLE IF NOT EXISTS words (word VARCHAR(20), tagalog VARCHAR(50), english VARCHAR(50))";
    113.                     _command.CommandText = sql;
    114.                     _command.ExecuteNonQuery();
    115.                    
    116.                     _command.CommandText = "INSERT INTO answered VALUES ('"+answer+"')";
    117.                     _connection.Open ();
    118.                     _command.ExecuteNonQuery();
    119.                     _connection.Close();
    120.                     nahulaan = true;
    121.                     x = "";  
    122.                 }
    123.                 if(nahulaan==true)
    124.                 {
    125.                     //DITO MO ILAGAY CODES PAG TUMAMA!!
    126.                     GUI.Box (new Rect (600, 280, 100, 30), "CORRECT!");
    127.                     GUI.Box (new Rect (600, 320, 200, 50), tagalog);
    128.                     GUI.Box (new Rect (600, 420, 200, 50), english);
    129.                 }
    130.             }
    131.            
    132.            
    133.         }
    134.         catch(IOException errr)
    135.         {
    136.             Debug.Log(errr.Message);
    137.         }
    138.        
    139.        
    140.        
    141.     }
    142.    
    143.     // Use this for initialization
    144.     void Start ()
    145.     {
    146.         completed = new bool[5];
    147.        
    148.         isAvailable = new ArrayList ();
    149.         toBeRemoved = new ArrayList ();
    150.         exampleFiles = new ArrayList ();
    151.        
    152.         string _strDBName = "URI=file:"+Application.dataPath+"/dbfile";
    153.        
    154.         IDbConnection _connection = new SqliteConnection(_strDBName);
    155.         IDbCommand _command = _connection .CreateCommand();
    156.         string sql;
    157.  
    158.         _connection.Open();
    159.        
    160.         sql = "CREATE TABLE IF NOT EXISTS words (word VARCHAR(20), tagalog VARCHAR(50), english VARCHAR(50))";
    161.         _command.CommandText = sql;
    162.         _command.ExecuteNonQuery();
    163.        
    164.         _command.CommandText = "SELECT * FROM words WHERE word not in (select word from answered) limit 1;";
    165.         _connection.Open ();
    166.         IDataReader _reader = _command.ExecuteReader ();
    167.         while (_reader.Read())
    168.         {
    169.             word = _reader.GetString(0);
    170.             tagalog = _reader.GetString(1);
    171.             english = _reader.GetString(2);
    172.         }          
    173.         _connection.Close ();
    174.        
    175.         for (int i=0; i<12; i++)
    176.             isAvailable.Add ("1");
    177.        
    178.         ArrayList lstExampleFiles = new ArrayList();
    179.         lstExampleFiles.AddRange( exampleFiles );
    180.         int d2 = Random.Range (0, lstExampleFiles.Count);
    181.         Debug.Log (word);
    182.         answer = word;
    183.         c = answer;
    184.         for (int i=0; i<c.Length; i++) {
    185.             letters.Add(c.Substring(i,1));
    186.         }
    187.         int rr = 0;
    188.         for (int i=letters.Count; i<12; i++) {
    189.             rr = Random.Range(0,l.Length);
    190.             letters.Add(l.Substring(rr,1));
    191.         }
    192.         letters = ShuffleList (letters);      
    193.         icon = new Texture[4];
    194.         for(int i=0; i<4;i++)
    195.         {
    196.             icon[i]= (Texture)Resources.Load(answer+"/"+(i+1));
    197.         }
    198.     }
    199.    
    200.     // Update is called once per frame
    201.     void Update () {
    202.        
    203.     }
    204.    
    205.     private ArrayList ShuffleList(ArrayList inputList)
    206.     {
    207.         ArrayList randomList = new ArrayList();
    208.        
    209.        
    210.         int randomIndex = 0;
    211.         while (inputList.Count > 0)
    212.         {
    213.             randomIndex = Random.Range (0, inputList.Count-1); //Choose a random object in the list
    214.             randomList.Add(inputList[randomIndex]); //add it to the new, random list
    215.             inputList.RemoveAt(randomIndex); //remove to avoid duplicates
    216.         }
    217.        
    218.         return randomList; //return the new random list
    219.     }
    220. }
     
  2. BmxGrilled

    BmxGrilled

    Joined:
    Jan 27, 2014
    Posts:
    239
    You would use Input.GetKeyDown(KeyCode c);
    returns a bool (true/false) true if the key is pressed;
    so like e.g.
    if (Input.GetKeyDown(KeyCode.Space))
    {
    //do some magic
    }
    You'd place this code inside the Update event by the way, hope this helps! :)
     
  3. Rbert

    Rbert

    Joined:
    Jul 13, 2014
    Posts:
    28
    can u try it with my code..
    i want if i press backspace the letter will be delete if i press enter will go to next word and i press esc will quit..and the letters also
     
  4. Garvindo8

    Garvindo8

    Joined:
    Jun 23, 2021
    Posts:
    1
    I want to input

    void Update()
    {
    if(Input.GetKey(KeyCode.Escape)) }
    Application.Quit

    But, the words "quit & Escape" that I'm typing about on Visual Studio,didn't work/didnt directly connect to the unity. How can I fix it?
     
  5. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,742
    First, please stop posting to unrelated threads from 2014. It's against forum rules.

    Instead, start your own post, it's FREE.

    If you are doing tutorials for this code, you cannot miss even ONE CHARACTER, and I can see you have missed a LOT of characters above. This purpose of this board is not to fix your typos.

    How to do tutorials properly:

    Tutorials are a GREAT idea. Tutorials should be used this way:

    Step 1. Follow the tutorial and do every single step of the tutorial 100% precisely the way it is shown. Even the slightest deviation generally ends in disaster. That's how software engineering works. Every single letter must be spelled, capitalized, punctuated and spaced (or not spaced) properly. Fortunately this is the easiest part to get right. Be a robot. Don't make any mistakes. BE PERFECT IN EVERYTHING YOU DO HERE.

    Step 2. Go back and work through every part of the tutorial again, and this time explain it to your doggie. See how I am doing that in my avatar picture? If you have no dog, explain it to your house plant. If you are unable to explain any part of it, STOP. DO NOT PROCEED. Now go learn how that part works. Read the documentation on the functions involved. Go back to the tutorial and try to figure out WHY they did that. This is the part that takes a LOT of time when you are new. It might take days or weeks to work through a single 5-minute tutorial. Stick with it. You will learn.

    Step 2 is the part everybody seems to miss. Without Step 2 you are simply a code-typing monkey and outside of the specific tutorial you did, you will be completely lost.

    Of course, all this presupposes no errors in the tutorial. For certain tutorial makers (like Unity, Brackeys, Imphenzia, Sebastian Lague) this is usually the case. For some other less-well-known content creators, this is less true. Read the comments on the video: did anyone have issues like you did? If there's an error, you will NEVER be the first guy to find it.

    Beyond that, Step 3, 4, 5 and 6 become easy because you already understand!
     
    wda and UnityMaru like this.
Thread Status:
Not open for further replies.