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

Finding objects by name when all have same name

Discussion in 'UGUI & TextMesh Pro' started by UnityMatt, Mar 19, 2015.

  1. UnityMatt

    UnityMatt

    Joined:
    Mar 7, 2015
    Posts:
    13
    I have a bunch of tiles(buttons) with letters and I need to save the letter from the selected button.

    Because they are all named "Text" it always finds the first of the buttons.

    Text letter;
    letter = GameObject.Find("Text").GetComponent<Text>();
    Global.selectedLetter = letter.text;

    The Global is public static
    The script is attached to each button.

    Any ideas.
     
  2. SimonDarksideJ

    SimonDarksideJ

    Joined:
    Jul 3, 2012
    Posts:
    1,685
    Well if they all have the same name when you place them, they you aren't going to find the name that way.
    You can alternatively set the TAG of the GO when it is instantiates and use FingGameObjectWithTag (got that from memory so might not be completely accurate :D)

    Also, if the user is "Selecting" the letter, you might want to simply create a script using the IPointerClickHandler interface attached to each button and then get the Text Component of the GO instance that is clicked.
    If you need a fuller example, let me know.
     
  3. UnityMatt

    UnityMatt

    Joined:
    Mar 7, 2015
    Posts:
    13
    "get the Text Component of the GO instance that is clicked."

    I was looking for way to do this. I thought I could use "this" but couldn't find anything to do it that way.


    Thanks
     
  4. SimonDarksideJ

    SimonDarksideJ

    Joined:
    Jul 3, 2012
    Posts:
    1,685
    OK, hopefully this will help clear things up :D
    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.EventSystems;
    3. using UnityEngine.UI;
    4.  
    5. public class ClickingText : MonoBehaviour, IPointerClickHandler
    6. {
    7.     private Text myText;
    8.     // Use this for initialization
    9.     void Start () {
    10.         myText = GetComponent<Text>();
    11.     }
    12.  
    13.     public void OnPointerClick(PointerEventData eventData)
    14.     {
    15.         myText.text = "My name is [" + gameObject.name + "] and my tag is [" + gameObject.tag + "]";
    16.     }
    17. }
    Attach that to a Text UI GO and in run mode, when you click it, it will set the text display to the GO's name and Tag :D
     
  5. UnityMatt

    UnityMatt

    Joined:
    Mar 7, 2015
    Posts:
    13

    Thanks, I got it to work. Another question though. If I have a class does it need to be attached to a game object?
    I'm trying to convert something I made with XNA a while back and have some classes that I need to implement. I need to use the classes to initialize the scenes and they need to be used by what would now be different scenes. Would I just make a blank game object?
     
    SimonDarksideJ likes this.
  6. rakkarage

    rakkarage

    Joined:
    Feb 3, 2014
    Posts:
    683
  7. SimonDarksideJ

    SimonDarksideJ

    Joined:
    Jul 3, 2012
    Posts:
    1,685
    :D Hmmm #XNA @UnityMatt

    Simple answer is no. As it is C# under the hood you can either NEW() up a class in a script or create a static utility script.

    Main difference is how you then access those if you need to. Static is available everywhere, A class property is only available if it's public in the script it is embedded within, no different to how XNA uses them really.

    The benefit of attaching a script to a GO (empty or otherwise) is that you get access to all the base MonoBehaviour stuff behind the scenes and the ability to pick just that script from a GO in it's use (Can also find all objects with that script easily with GetComponentsInChildren<script>()

    Hope that helps

    btw (slightly off topic :D)
    Sis you see this?
    http://darkgenesis.zenithmoon.com/xna-is-no-more-as-the-phoenix-rises-from-the-ashes/