FindGameObjectWithTag wont work [SOLVED]

Hello i have a problem with FindGameObjectWithTag
Here are the script:

        if (gun == null) {
            Debug.Log("gunIsNull");
            GameObject.FindGameObjectWithTag("Gun");
        }

        if (kit == null) {
            Debug.Log("kitIsNull");
            GameObject.FindGameObjectWithTag("Kit");
        }

        if (munizion == null) {
            Debug.Log("munIsNull");
            GameObject.FindGameObjectWithTag("Munizion");

Here is the spawnScript:

 random = Random.Range(0, 4);
        switch (random) {
            case 0:
            //Spawn Kit
            Instantiate(kit[0], pos[2].position, Quaternion.identity);
            //Spawn Munizion
            Instantiate(munizion, pos[1].position, Quaternion.identity);
            //Spawn Guns
            Instantiate(guns[0], pos[0].position, Quaternion.identity);
            break;
            case 1:
            //Spawn Kit
            Instantiate(kit[1], pos[2].position, Quaternion.identity);
            //Spawn Munizion
            Instantiate(munizionSniper, pos[1].position, Quaternion.identity);
            //Spawn Guns
            Instantiate(guns[1], pos[0].position, Quaternion.identity);
            break;
            case 2:
            //Spawn Kit
            Instantiate(kit[0], pos[2].position, Quaternion.identity);
            //Spawn Munizion
            Instantiate(munizion, pos[1].position, Quaternion.identity);
            //Spawn Guns
            Instantiate(guns[2], pos[0].position, Quaternion.identity);
            break;
            case 3:
            //Spawn Kit
            Instantiate(kit[1], pos[2].position, Quaternion.identity);
            //Spawn Munizion
            Instantiate(munizionSniper, pos[1].position, Quaternion.identity);
            //Spawn Guns
            Instantiate(guns[3], pos[0].position, Quaternion.identity);
            break;
        }

But it doesn’t work. I think because it spawns objects. But I do inside the void Update and can it not find spawned object with a tag?
Thanks

How do you know if it’s working or not? You’re completely ignoring the result!

For example, wouldn’t you want to do this?gun = GameObject.FindGameObjectWithTag("Gun");

Second of all, why do you need to use FindGameObjectWithTag at all? There’s no need for it if you’re Instantiating the objects in this very script. You are similarly ignoring the result of Instantiate. You could just do this:
gun = Instantiate(guns[1], pos[0].position, Quaternion.identity);Then you can get rid of the FindGameObjectWithTag entirely.

1 Like

Sorry for this. But i solved it Thanks for reading

Thanks it works

Another thing - there’s absolutely no reason to be using a switch statement here. That whole thing can be replaced like this:

random = Random.Range(0, 4);
kit = Instantiate(kit[random], pos[2].position, Quaternion.identity);
munizion = Instantiate(random % 2 == 0 ? munizion : munizionSniper, pos[1].position, Quaternion.identity);
gun = Instantiate(guns[random], pos[0].position, Quaternion.identity);
1 Like

Thanks