How do you have a object detect different types of tags but have the same output

I can’t use the II operators with strings but I want my script to detect multiple types of tags but it doesn’t work. How do I fix this in my script?

using UnityEngine;
using System.Collections;

public class Jump : MonoBehaviour
{
public bool OnGround;
private Rigidbody rb;

void Start()
{
    OnGround = true;
    Debug.Log("OnGround set to true in Start");
    rb = GetComponent<Rigidbody>();
}

void Update()
{
    if (OnGround)
    {
        if (Input.GetButton("Jump"))
        {
            rb.velocity = new Vector3(0.0f, 10f, 0.0f);
            OnGround = false;
            Debug.Log("OnGround set to false in Update");
        }
        else
        {
        }
    }
}

void OnCollisionEnter(Collision other)
{
    Debug.Log("Collision entered with tag named: " + other.gameObject.tag);

    if (other.gameObject.CompareTag("Ground"))
    {
        OnGround = true;
        Debug.Log("OnGround Set To true in OnCollisionEnter, collided with tag: " + other.gameObject.tag);
    }
    else
    {
        OnGround = false;
        Debug.Log("OnGround Set To false in OnCollisionEnter, collided with tag: " + other.gameObject.tag);
    }
}

}

Hey @dragonking300,

If I understood correctly, you want to check multiple tags at once!
Try adding a list with tags, and a method that checks if a certain string (tag) is in that list.

Which would look something like this:

using UnityEngine;
using System.Collections;
using System.Collections.Generic; // NEW

public class Jump : MonoBehaviour
{
    public bool OnGround;
    private Rigidbody rb;

    // NEW
    private List<string> tagsToCompare = new List<string>()
    {
        "Ground",
        "tag2",
        "tag3"  // Note how the last one doesn't need a comma.
    };

    void Start()
    {
        OnGround = true;
        Debug.Log( "OnGround set to true in Start" );
        rb = GetComponent<Rigidbody>();
    }
    void Update()
    {
        if( OnGround )
        {
            if( Input.GetButton( "Jump" ) )
            {
                rb.velocity = new Vector3( 0.0f, 10f, 0.0f );
                OnGround = false;
                Debug.Log( "OnGround set to false in Update" );
            }
            else
            {
            }
        }
    }
    void OnCollisionEnter( Collision other )
    {
        Debug.Log( "Collision entered with tag named: " + other.gameObject.tag );
        if( isTargetTag( other.gameObject.tag ) ) // NEW
        {
            OnGround = true;
            Debug.Log( "OnGround Set To true in OnCollisionEnter, collided with tag: " + other.gameObject.tag );
        }
        else
        {
            OnGround = false;
            Debug.Log( "OnGround Set To false in OnCollisionEnter, collided with tag: " + other.gameObject.tag );
        }
    }

    // NEW
    private bool isTargetTag( string tagToCheck )
    {
        return tagsToCompare.Contains( tagToCheck );
    }
}

I added // NEW comments in places where I changed your code.
I hope that helps, if it did, please accept this answer, it’d be much appreciated!
If you need any more details, let me know! :slight_smile:

Cheers,

ThePersister