Search and Destroy

Hi, Happy Holidays everyone!

I have instantiated lots of textmesh objects in the scene and am wondering if I can search and destroy all text meshes which include a certain string or have no string at all. Would that be text.length == 0 or if text == “some text”

I can destroy the prefab of the text mesh while I type into it, by pressing the backspace key until mytext.text.Length == 0

What I need is, if no input is entered into the text mesh it is destroyed automatically, so no empty prefabs mess up the scene.

Thanks

How does this sound -

Attach the following script to a singleton object (game manager) -

function Start() {
  var allTextMeshes : Array = GameObject.FindObjectsOfType( TextMesh );
  for ( var tm : TextMesh in allTextMeshes ) {
    if ( tm.text.length == 0 ) Destroy(tm.gameObject);
  }
}

You can use either text.length or text == “”. Personally I find it clearer to use .length, but it’s really personal preference.

Hi Vicenti

Thanks for the reply. I think I know what the problem is, just not how to solve it. When I instantiate the text mesh in the game, each text mesh objects starts it’s life with 0 characters. I then type into the mesh. However sometimes a text mesh may be instantiated by the user clicking and then no text is added, so it remains as 0 characters. My question, is how do I clean up those meshes. The Start function doesn’t seem to work at all, because I guess no text meshes exist at the start. However your code in an update function destroys all text meshes, disregarding whether they have 0 characters or more. I have added a timer to the function to destroy after five seconds. My idea is, if the character count remains at 0 for twenty seconds after instantiation, this text mesh is destroyed. Not sure how to do that.

Thanks for your help.

For anyone who is interested, I’ve added this Cleaner function to the end of my text code and it seems to work. So if no input is detected and the character length remains at 0 then it gets destroyed. No more empty prefab instances in the scene.

function Cleaner(){
var mytext = this.gameObject.GetComponent(TextMesh);
yield WaitForSeconds(10);
if (mytext.text.length == 0)
Destroy(mytext.gameObject);
}