Getcomponent error?

Let’s say I have an object A, and an object B.

B contains a script called “Test” right?
B has a tag called “Player”

Now in A, on my start method, it does this:

if (GameObject.FindGameObjectWithTag("Player").GetComponent<Test>() == null)
            print("null");

In this case, A will not print out null because B has that component.

Now let’s say I create an object C.

C is the same as b, however its tag will be player2. It still has Test component in it.

object A will now search for a gameobjectwithtag of “player2”.

Problem is, null will be printed to the console.

As you can see in this scenario above, it’s pretty weird. I’m having this situation right now and I think it might have to do with all the gameobjects scripts being loaded in or something. It’s weird because object A can find object B but not object C.

Let me know if I have to clear up on anything, this has been driving me crazy!

Do Debug.Log(GameObject.FindGameObjectWithTag("player2")) and I am pretty sure you will see the wrong game object printed. Check the tags, you might have set the same tag on other object too.
Also, try to avoid finding by tag or by name since it is slow and unreliable. The same performance problem happens with GetComponent. These functions are the killers of performance.