Find GameObject with tag error

In update, I wrote it to be where it searches for a gameobject with a tag “Enemy” on it.
but on start, nothing has that tag. I can play the game fine, but it just bugs me how the console keeps reading me errors. It says object reference not set to an instance of an object. This is what I wrote:

fpsTarget = GameObject.FindWithTag(“Enemy”).transform;
if (fpsTarget == null)
{

}
else
{
fpsTargetDistance = Vector3.Distance(fpsTarget.position, transform.position);

Is fpsTarget of type Transform

Transform fpsTarget;

in the line
“if (fpsTarget = null)”
make sure you are using a compare ’ == ’ not the = operator which would set fpsTarget equal to null

1 Like

just to extend on this, if you use an assignment in a conditional like you have it’ll return “true” if it manages to succeed in that assignment, so in most instances it’ll always be returning true rather than throwing any errors.

https://forum.unity3d.com/threads/using-code-tags-properly.143875/ :slight_smile:

1 Like

Yep this is why a reverse check is preferable(programmers can get sleepy) as that will flag a compiler error if only one = is used !

I mean using something like this :

if (null == fpsTarget)

the easiest is

if(fpstarget)
//and
if(!fpstarget)

but then it makes people less sensitive to comparisons and assignments :eyes:

Nope!

Assignment just returns the assigned value. So for the statement:

GameObject result = (fpsTarget = null);

result will be null.

But GameObject (and all UnityEngine.Objects) have an implicit cast to bool. So this:

fpsTarget = GameObject.FindWithTag("Enemy").transform;
if (fpsTarget = null)
{

}
else
{
    fpsTargetDistance = Vector3.Distance(fpsTarget.position, transform.position);
}

resolves to:

fpsTarget = GameObject.FindWithTag("Enemy").transform;
fpsTarget = null;
bool asBool = (bool) fpsTarget);
if(asBool) {

}
else {
    fpsTargetDistance = Vector3.Distance(fpsTarget.position, transform.position);
}

Having = return a value allows you to do things like:

int a, b, c, d;
a = b = c = d = 5;

Which is nice. On the other hand, there’s a lot of bugs from people using = instead of ==. The implicit cast from Object to bool makes this worse.

The problem with that is that it only works for Unity-Objects. So you can’t do:

string s = null;
if(s) { //Won't compile!

}
1 Like

exactly , it applies only the context of this unity related question , and unless implemented wont work , therefore becoming used to unity having the conversion check wil make you prone to making the mistake with those not implimented , but when you know the differences theres no issue .

Is it possible if I just exported the game and posted it on here? so you guys could just tell me what you would do to fix the problem?

the problem is, that just the console reading unatural errors. I can play the game just fine.
In void update, I make the npc search for a gameobject with a tag “Enemy” on it. But there wouldnt be a gameobject with the tag “Enemy” on it until I make an enemy.

so meanwhile, how should I satisfy the function that searches for the “Enemy” object?

I dunno how this thread got so derailed but - you’re trying to access the transform property from finding something tagged with Enemy. It doesn’t find anything so it throws an error. Split your Find(…) and .transform into two lines and check if the result of the Find() is null before proceeding.

1 Like