Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Array elements script

Discussion in 'Scripting' started by veleno94, Dec 5, 2015.

  1. veleno94

    veleno94

    Joined:
    Dec 5, 2015
    Posts:
    23
    Hello everyone!
    I would like to create a script that performs this procedure:
    1) The user enters a letter in the inputfield, press the "GO" button that inserts a letter in the first position of an array;
    2) Is automatically selected one letter at random, which is inserted in the second position of the array.
    The procedure is repeated until the user presses the "SHOW" button, which will show on the screen the whole word.
    For now I wrote this script (and obviously it doesn't work):
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine.UI;
    5.  
    6.  
    7. public class TestScript : MonoBehaviour {
    8.     string[] word = new string[10];
    9.     public InputField iField;
    10.     private string inputLetter;
    11.     string[] letters = {"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z" };
    12.  
    13.     public void OnClick_GO(){
    14.  
    15.                 for (int i = 0; i==10; i=i+2) {
    16.                     inputLetter = iField.text;
    17.                     word [i] = inputLetter;
    18.                     Debug.Log(word[i]);
    19.                     word [i+1] = letters[Random.Range(0,26)];
    20.                     Debug.Log(word[i+1]);
    21.                 }
    22.     }
    23.        
    24. }
     
  2. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    that for syntax is wrong... and you also don't need a loop here.

    According to your specification, when the user pushes the button you need to insert the inputfield letter and insert a random selected letter. That's it, two operations. The only other thing you are going to need to do is keep track of how many times the button as been pressed so you can change the index the letter are being inserted to (or you might just want to switch to using a list and the .Add() function)
     
  3. veleno94

    veleno94

    Joined:
    Dec 5, 2015
    Posts:
    23
    Thank you for your answer, but I didn't understand how to keep track of how many times the button has been pressed. Can you make me an example, please?
     
  4. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    Code (csharp):
    1.  
    2. private int times = 0;
    3.  
    4. // in button script
    5. times+=1;
    6.  
     
  5. veleno94

    veleno94

    Joined:
    Dec 5, 2015
    Posts:
    23
    Thank you so much!
     
  6. veleno94

    veleno94

    Joined:
    Dec 5, 2015
    Posts:
    23
    @LeftyRighty I have another question.
    I wish, if the "v" button is pressed, the word "finalWord" is searched in a wordlist with the extension .txt. If the word is present in the wordlist the script says "the word exists". For now my script is this:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine.UI;
    5. using System.IO;
    6.  
    7.  
    8. public class TestScript : MonoBehaviour {
    9.     string[] word = new string[20];
    10.     public InputField iField;
    11.     private string inputLetter;
    12.     public Text showWord;
    13.     private int times = 0;
    14.     string[] letters = {"a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"};
    15.  
    16.     private string text;
    17.  
    18.     void Start(){
    19.         text = File.ReadAllText (@"C:\Users\MyPC\Documents\MyGame\Assets\words.italian.txt");
    20.     }
    21.  
    22.     public void OnClick_GO(){
    23.         times = times + 1;
    24.         inputLetter = iField.text;
    25.         word [times] = inputLetter;
    26.         Debug.Log(word[times]);
    27.         word [times+1] = letters[Random.Range(0,26)];
    28.         Debug.Log(word[times+1]);
    29.  
    30.         string finalWord = string.Join ("", word);
    31.         Debug.Log ("Word: " + finalWord);
    32.         showWord.text = finalWord;
    33.  
    34.     public void OnClick_V(){
    35.    
    36.     }
    37.  
    38.     }