Search Unity

Destroy TextMesh from generate objects

Discussion in '2D' started by IsidroPerla, Jul 31, 2020.

?

Destroy TextMesh from generate objects

  1. Textmesh methods

    0 vote(s)
    0.0%
  2. Acces to before generate objects

    0 vote(s)
    0.0%
  1. IsidroPerla

    IsidroPerla

    Joined:
    Jul 30, 2020
    Posts:
    1
    Hi, I would like to know how can I destroy a textmesh in objects generates for example a have
    prefabricated a block with a textMesh, and I generate news with a class, but I only can destroys current blocks in my logic class, how can I destroy the befores textmesh??


    This is my blocks generator class

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class GeneratorLogic : MonoBehaviour
    6. {
    7.  
    8.     private string displayText;
    9.     private TextMesh testMesh;
    10.     public GameObject[] blocks;
    11.  
    12.     // Start is called before the first frame update
    13.     void Start()
    14.     {
    15.         NewBlock();
    16.     }
    17.  
    18.     // Update is called once per frame
    19.     void Update()
    20.     {
    21.  
    22.     }
    23.  
    24.     public void NewBlock()
    25.     {
    26.         Instantiate(blocks[Random.Range(0, blocks.Length)], transform.position, Quaternion.identity);
    27.     }
    28.  
    29. }
    and this is my logic class

    And each time This program generates new blocks with textmesh, and I only can destroy the current block how can I destroy the befores generates blocks

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

    using Mono.Data.Sqlite;
    using System.Data;
    using System.Linq;

    public class BlocksLogic : MonoBehaviour
    {
    //
    private string displayText;
    private TextMesh testMesh;
    //

    private float tiempoAnterior;
    public float tiempoCaida = 0.8f;

    public static int alto = 15;
    public static int ancho = 9;

    public Vector3 puntoRotation;

    private static Transform[,] grid = new Transform[ancho, alto];

    //
    int var_random;
    //

    // Start is called before the first frame update
    void Start()
    {
    }

    // Update is called once per frame
    void Update()
    {

    if (Input.GetKeyDown(KeyCode.LeftArrow))
    {
    transform.position += new Vector3(-3, 0, 0);
    if (!Limites())
    {
    transform.position -= new Vector3(-3, 0, 0);
    }
    }

    if (Input.GetKeyDown(KeyCode.RightArrow))
    {
    transform.position += new Vector3(3, 0, 0);
    if (!Limites())
    {
    transform.position -= new Vector3(3, 0, 0);
    }
    }

    if (Time.time - tiempoAnterior > (Input.GetKey(KeyCode.DownArrow) ? tiempoCaida / 20 : tiempoCaida))
    {
    transform.position += new Vector3(0, -1, 0);
    if (!Limites())
    {
    transform.position -= new Vector3(0, -1, 0);

    AñadirAlGrid();

    RevisarLineas();

    this.enabled = false;
    FindObjectOfType<GeneratorLogic>().NewBlock();
    }
    tiempoAnterior = Time.time;
    }

    }

    bool Limites()
    {

    foreach (Transform hijo in transform)
    {
    int enteroX = Mathf.RoundToInt(hijo.transform.position.x);
    int enteroY = Mathf.RoundToInt(hijo.transform.position.y);

    if (enteroX < 0 || enteroX >= ancho || enteroY < 0 || enteroY >= alto)
    {
    return false;
    }

    if (grid[enteroX, enteroY] != null)
    {
    return false;
    }

    }

    //
    position[Mathf.RoundToInt(transform.position.x) , Mathf.RoundToInt(transform.position.y)] = 2;

    Debug.Log(position[4,0].ToString());

    return true;

    }

    void AñadirAlGrid()
    {

    foreach (Transform hijo in transform)
    {
    int enteroX = Mathf.RoundToInt(hijo.transform.position.x);
    int enteroY = Mathf.RoundToInt(hijo.transform.position.y);

    grid[enteroX, enteroY] = hijo;
    }

    }

    void RevisarLineas()
    {
    for (int i = alto - 1; i >= 0; i--)
    {
    if (TieneLinea(i))
    {
    BorrarLinea(i);
    BajarLinea(i);
    }
    }
    }

    bool TieneLinea(int i)
    {
    for (int j = 0; j < ancho; j++)
    {
    if (grid[j, i] == null)
    {
    return false;
    }
    }
    return true;
    }

    void BorrarLinea(int i)
    {
    for (int j = 0; j < ancho; j++)
    {
    Destroy(grid[j, i].gameObject);
    Destroy(GetComponent<TextMesh>()); ?????????????
    How can I destroy the before objects with textmesh??
    grid[j, i] = null;
    }
    }

    void BajarLinea(int i)
    {
    for (int y = i; y < alto; y++)
    {
    for (int j = 0; j < ancho; j++)
    {
    if (grid[j, y] != null)
    {
    grid[j, y - 1] = grid[j, y];
    grid[j, y] = null;
    grid[j, y - 1].transform.position -= new Vector3(0, 1, 0);
    }
    }
    }
    }

    }