- Home /
Find Keyword in a Searched GameObject (JS)
Hello, I've currently got a complex turn based game which I working on but I need a quick bit of help with this dilemma.
How do I find and then destroy a gameObject with a keyword in it?
I've got something like:
if (GameObject.Find(KeywordA in this name of the gameObject)) {
then go ahead and Destroy(this gameObject);
}
So if I have many objects named "TestingObject5 Movement=(A different random number)" and a few named "TestingObject4 Movement=(A different random number)", and KeywordA is "TestingObject5", it will destroy all the objects with the name of "TestingObject5 Movement=(A different random number)".
Thanks for the help.
Answer by Lemo76 · Oct 28, 2014 at 08:59 AM
Ended up using:
var SceneObj : GameObject [];
function FixedUpdate () {
SceneObj = FindObjectsOfType(GameObject) as GameObject[];
}
function CreateTurnObj () {
for (var i=0; i < AllSceneObj.length; i++){
if(SceneObj[i].name.Contains("Turn=" + TurnPlace)){
print (SceneObj[i].name);
}}}
The one restriction is it can cause lag (i haven't noticed it) if this is used on a scene with a lot of objects.
Answer by MarksTeq · Oct 26, 2014 at 10:55 AM
You can use tags and then do something like:
public Object yourObjectNames;
yourObjectName= GameObject.FindGameObjectsWithTag("Respawn");
foreach (Object yourObjectName in yourObjectNames) {
Destroy(yourObjectName,0.0F);
}
Tags do work, however since in the game I'm instantiating a new set of "TestingObject"'s per turn, it would mean I have to declare up to 99 different tags for each of them.
That's why I'm looking for a way to use the "$$anonymous$$eyword". Thanks.
@Lemo76 Here's a similar question that address your problem Finding Game objects with substring
Your answer
Follow this Question
Related Questions
using Contains(gameObject) to find and destroy a gameObject from a list 2 Answers
"GameObject.Find();" not working 2 Answers
Destroy on Collision? 1 Answer
"Destroy Game Object" Not Destroying Game Object 3 Answers
Simple In game search field 2 Answers