Search Unity

  1. We are migrating the Unity Forums to Unity Discussions. On July 12, the Unity Forums will become read-only.

    Please, do not make any changes to your username or email addresses at id.unity.com during this transition time.

    It's still possible to reply to existing private message conversations during the migration, but any new replies you post will be missing after the main migration is complete. We'll do our best to migrate these messages in a follow-up step.

    On July 15, Unity Discussions will become read-only until July 18, when the new design and the migrated forum contents will go live.


    Read our full announcement for more information and let us know if you have any questions.

The same text on all items during keyboard input on Android.

Discussion in 'Scripting' started by u_rs, Apr 10, 2018.

  1. u_rs

    u_rs

    Joined:
    Jan 5, 2016
    Posts:
    147
    Description of functionality of my project:
    I have AddButton on screen. When I click on it there new item appear on screen. I click again - one more item.
    I should be able to change text on each of those items by clicking on it: software keyboard opens and I enter text.

    Problem:
    If I click on first item - it's ok. But when I click on second one I notice that text on previously clicked item being changed too. As if they would share one text. But text on items should be independent.

    There 2 classes in script file.
    One controls AddButton and another class gonna be attached to newly created item on button click.

    I would very appreciate if someone would take a look. Thanks.

    This is script:
    Code (CSharp):
    1. public class AddItem : MonoBehaviour {
    2.     ...
    3.     void Start () {
    4.         buttons = new ArrayList();
    5.          coordY = 400;
    6.         addButton = this.GetComponent<Button>();
    7.         addButton.onClick.AddListener(AddButtonClicked);
    8.     }
    9.     void AddButtonClicked () {
    10.         ...
    11.         buttons.Add(newButton);
    12.     }
    13. ...
    14. }
    15.  
    16. public class ItemController : MonoBehaviour {
    17. ...
    18.     void Start () {
    19.         thisItem = this.GetComponent<Button>();
    20.         thisItem.onClick.AddListener(OpenKeyboard);
    21.     }
    22.  
    23.     void Update() {
    24.         if (keyboard != null && keyboard.active)
    25.             thisItem.GetComponentInChildren<Text>().text = keyboard.text;
    26.     }
    27.  
    28.     void OpenKeyboard() {
    29.         keyboard = TouchScreenKeyboard.Open("", TouchScreenKeyboardType.ASCIICapable, false, false, false, false);  
    30.         thisItem.GetComponentInChildren<Text>().text = keyboard.text;
    31.     }
    32. }
     
    Last edited: Apr 12, 2018
  2. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,200
    Eww....Arraylist...ok, I'll save judgement :)

    The issue is your Update. How does Update know which Text is suppose to change?

    Right now, all your buttons run Update and check if keyboard != null && keyboard.active.

    So as soon as those two values are true, the button will update it's text.

    You'll need a way to designate if it's not the target button that you clicked on last, to not update it's text. However, it wouldn't surprise me if there is a better way to do this where update isn't required.
     
    u_rs likes this.
  3. u_rs

    u_rs

    Joined:
    Jan 5, 2016
    Posts:
    147
    Thank you very much Brathnann ! What about Arraylist :D ?
     
  4. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,200
    Arraylist are a catch all type of collection where it doesn't care what you put in it and requires casting to get out of it. Generally you could stick a GameObject, a string, an int, a sprite, etc into it and it wouldn't care.

    Honestly, it's much better to just use a Generic list.

    Code (csharp):
    1. List<GameObject> buttons = new List<GameObject>();
    Would be better for your example.
     
    u_rs likes this.
  5. u_rs

    u_rs

    Joined:
    Jan 5, 2016
    Posts:
    147
    Thanks.