Search Unity

Identifying Text on the block(TextMesh)

Discussion in 'Scripting' started by Ki4Chan, Sep 21, 2017.

  1. Ki4Chan

    Ki4Chan

    Joined:
    Jul 2, 2017
    Posts:
    24
    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?
     
  2. Rob21894

    Rob21894

    Joined:
    Nov 21, 2013
    Posts:
    309
    Have you attempted this? Can you provide any code/Example of what you want?
     
  3. Ki4Chan

    Ki4Chan

    Joined:
    Jul 2, 2017
    Posts:
    24
    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?
     
  4. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,188
    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.
     
  5. Rob21894

    Rob21894

    Joined:
    Nov 21, 2013
    Posts:
    309
    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

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class RandomNumberGuesser : MonoBehaviour
    6. {
    7.  
    8.     public GameObject prefab;
    9.     public int amountToSpawn = 4;
    10.     private int TargetNumber = 0;
    11.     public int[] numbers;
    12.     private Vector3 lastTransform;
    13.  
    14.     // Use this for initialization
    15.     void Start ()
    16.     {
    17.         lastTransform = new Vector2(transform.position.x - 3, transform.position.y - 2);
    18.            numbers = new int[amountToSpawn];
    19.         for (int i = 0; i < amountToSpawn; i++)
    20.         {
    21.             numbers[i] = Random.Range(0, 1000);
    22.         }
    23.         TargetNumber = numbers[Random.Range(0, amountToSpawn)];
    24.         GameObject mainNumber = (GameObject)Instantiate(prefab, transform.position, transform.rotation);
    25.         mainNumber.GetComponent<TextMesh>().text = TargetNumber.ToString();
    26.         mainNumber.name = "Main Number";
    27.  
    28.         for (int i = 0; i < amountToSpawn; i++)
    29.         {
    30.             GameObject go = Instantiate(prefab,
    31.                 new Vector3(lastTransform.x + 1f, lastTransform.y, lastTransform.z), Quaternion.identity);
    32.             go.GetComponent<TextMesh>().text = numbers[i].ToString();
    33.             lastTransform = go.transform.position;
    34.         }
    35.  
    36.     }
    37.  
    38.     // Update is called once per frame
    39.     void Update ()
    40.     {
    41.         if (Input.GetMouseButtonDown(0))
    42.         {
    43.             RaycastHit2D hit = Physics2D.Raycast(Camera.main.ScreenToWorldPoint(Input.mousePosition), Vector2.zero);
    44.             if (hit.collider.tag == "Block")
    45.             {
    46.                 if (hit.collider.gameObject.GetComponent<TextMesh>().text == TargetNumber.ToString())
    47.                 {
    48.                     Debug.Log("Hit Target Number");
    49.                 }
    50.                 else
    51.                 {
    52.                     Debug.Log("Not Target Number");
    53.                     Destroy(hit.collider.gameObject);
    54.                 }
    55.             }
    56.         }
    57.  
    58.     }
    59. }
    60.  

    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
     

    Attached Files:

  6. Ki4Chan

    Ki4Chan

    Joined:
    Jul 2, 2017
    Posts:
    24
    I think I got some hints from this. thanks a lot.
     
  7. Ki4Chan

    Ki4Chan

    Joined:
    Jul 2, 2017
    Posts:
    24
    Yes, that's what Rob just showed. But I think I need to know more about TextMesh before proceeding to other things.