Identifying Text on the block(TextMesh)

Instantiating blocks having random numbers. On clicking on a block destroys the block but I couldn’t find a way to get(save in a variable, like “private int numberOnDestroyedBlock”) the number on that block. Anyone know how to do this?

Have you attempted this? Can you provide any code/Example of what you want?

I

I tried a lot but didn’t get any hint how to start this. Assume a number match game. One number(main) spawned then below it four other numbers(options) spawned now you have to click on the same number as above. So how do you check if the numbers are same or not. Is there a way to get the number on TextMesh when you click on it?

I would suggest you have a simple script on the blocks that tracks what number is displayed and then when clicked, you compare that number. You should also be able to access the text displayed with TextMesh, but I haven’t used Unity’s TextMesh, so the only thing I can say is if you are assigning a value to the TextMesh, you should be able to retrieve it through the same value.

Alright, I made a quick project using Unity 2D to demonstrate what I think you want to achieve the package file is attatched which you can open up in a seperate unity project.

also the code is here

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class RandomNumberGuesser : MonoBehaviour
{

    public GameObject prefab;
    public int amountToSpawn = 4;
    private int TargetNumber = 0;
    public int[] numbers;
    private Vector3 lastTransform;

    // Use this for initialization
    void Start ()
    {
        lastTransform = new Vector2(transform.position.x - 3, transform.position.y - 2);
           numbers = new int[amountToSpawn];
        for (int i = 0; i < amountToSpawn; i++)
        {
            numbers[i] = Random.Range(0, 1000);
        }
        TargetNumber = numbers[Random.Range(0, amountToSpawn)];
        GameObject mainNumber = (GameObject)Instantiate(prefab, transform.position, transform.rotation);
        mainNumber.GetComponent<TextMesh>().text = TargetNumber.ToString();
        mainNumber.name = "Main Number";

        for (int i = 0; i < amountToSpawn; i++)
        {
            GameObject go = Instantiate(prefab,
                new Vector3(lastTransform.x + 1f, lastTransform.y, lastTransform.z), Quaternion.identity);
            go.GetComponent<TextMesh>().text = numbers[i].ToString();
            lastTransform = go.transform.position;
        }

    }
  
    // Update is called once per frame
    void Update ()
    {
        if (Input.GetMouseButtonDown(0))
        {
            RaycastHit2D hit = Physics2D.Raycast(Camera.main.ScreenToWorldPoint(Input.mousePosition), Vector2.zero);
            if (hit.collider.tag == "Block")
            {
                if (hit.collider.gameObject.GetComponent<TextMesh>().text == TargetNumber.ToString())
                {
                    Debug.Log("Hit Target Number");
                }
                else
                {
                    Debug.Log("Not Target Number");
                    Destroy(hit.collider.gameObject);
                }
            }
        }

    }
}

What it does is generate random numbers and stores them, then chooses a target number out of them which is on the main object, then instantiates as many prefabs you want in the set locations and uses GetComponent functionallity to assign numbers to the text mesh and one will always be identical to the target number.

It then casts a raycast from the mouse position and detects if the ray hits the collider and checks for a number match, if a match occurs it prints hit target number, if it doesnt occur it deletes the gameobject and prints not target number

3228664–247739–RandomNumberGuesserPackage.unitypackage (4.03 KB)

I think I got some hints from this. thanks a lot.

Yes, that’s what Rob just showed. But I think I need to know more about TextMesh before proceeding to other things.