- Home /
Question by
Jonathan 5 · Feb 27, 2011 at 09:45 PM ·
booleanfindwithtag
FindWithTag or how to find if there any instances left
Hello. i'm trying to find it in searches but fail.
FindWithTag returns a null which in if() clause interpreted as false. what i can use so to check if there any instances of the same prefab left in the scene.
i just used this line
if(!(!GameObject.FindWithTag("Target"))){}
works fine but maybe there is a better way
Comment
Best Answer
Answer by Mike 3 · Feb 28, 2011 at 12:28 AM
You can use
if (GameObject.FindWithTag("Target") != null)
{
//objects left
}
or, if you intend to use the gameobject
var go = GameObject.FindWithTag("Target");
if (go != null)
{
//objects left, can do stuff with go
}
You can also skip the != null entirely, but that loses some of the description of what it's doing
Your answer
![](https://koobas.hobune.stream/wayback/20220613110543im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Randomly setting a boolean to true? 3 Answers
Can't translate bool from C# to unityscript 2 Answers
Performance Question, Change Boolean or Enable-Disable Script 0 Answers
Open and close doors by pressing 'E' 2 Answers
Why is this boolean value changing? 3 Answers