[Solved]Count objects with tag on OnTriggerEnter ?

How to count object with tag using OnTriggerEnter ?

I used this script, but it didn't give me correct number.

    public int weapons= 0;

    void OnTriggerEnter(Collider other)

    {

        print(other.gameObject.name);

        if (other.gameObject.tag == "weapons")

        {

            weapons++;

        }

    }

    void OnTriggerExit(Collider other)

    {

        if (other.gameObject.tag == "weapons")

        {

            weapons--;

        }

    }

  • Hunter & Tiff Eidmann(eithman) replied

    I believe the issue is that you have incorrectly typed out the code for comparing the tag.  Instead of 

    
    
     if (other.gameObject.tag == "weapons")
    
    

    try this instead.

    
    if (other.CompareTag("weapons"))


    Here is a link to the Unity page on that line of code. 

    https://docs.unity3d.com/ScriptReference/GameObject.CompareTag.html

  • Jonathan Gonzalez(jgonzalez) replied

    eithman Yep this is correct. I haven't tested the code in the first post, but that is the first thing I would change. If that still doesn't work, then make sure you're using a rigidbody on either the trigger or the object entering the trigger and that the tag has been properly assigned. 

  • AeleaS Omer(aeleas) replied


    eithman , this doesn't change a thing. still i get wrong number. example if there is one weapon I get 2 value ?

  • Jonathan Gonzalez(jgonzalez) replied

    aaeleas if you're getting 2 that means it's detecting it twice. Do you have the script on just the trigger? OnTriggerEnter only gets called once, same with OnTriggerExit. Here is what you should have:


    public int weapons;
        void OnTriggerEnter (Collider other)
        {
            if(other.CompareTag("weapons"))
                weapons++;
        }
    
  • AeleaS Omer(aeleas) replied


    jgonzalez , OK. If gun is around player count it +1. if player moved and gun is not around player it should count it  -1.. That is what i need to do. I don't know why script count it wrong !? 

  • Jonathan Gonzalez(jgonzalez) replied

    aaeleas I'd need to know more about how you have all this set up. I assume you have a trigger on the player that moves with him? It detects when a weapon is within the trigger zone correct? Is there a reason why you need it to count up or down? Why not just make it a bool to true or false. If the weapon is within range, set a bool to true, otherwise set it to false. 

    Also just to be clear, when you say "-1" are you saying subtract by one or actually go down to "-1"?

  • AeleaS Omer(aeleas) replied


    jgonzalez . It detects when a weapon is within the trigger zone correct? Yes Is there a reason why you need it to count up or down?  I want player knows how many weapons he can grab near him.

    Zero  means no gun on player zone. If there are 3 guns on player zone that means 3 weapons. He moved a bit forwarded and 2 guns on his zone, weapons value = 2. and so on. I wrote -1 to tell you zero. No need to any value under zero. Just count correctly the number of guns inside player zone. Thats all.

  • AeleaS Omer(aeleas) replied


    aaeleas , I solved. There is not issue with your scripts guys. I think it's from guns objects or tag function !!. What i did to solve this I put empty_object and named "handle'. add collider, radius 1.3.  change my script from using tag to name. Done ^-^. thank you guys.