Hi
i have a destroy ground which destroy objects hit it , but i want to make exception for some objects or tag the objects i want to destroy , how can i put tags for this code ?
using UnityEngine;
using System.Collections;
public class DestroyGround : MonoBehaviour {
void OnTriggerEnter2D(Collider2D collisionObject){
Destroy (collisionObject.gameObject);
}
}
note : they are multible objects i want to tag
Is it what you are asking for ?
and to check a tag :
if(collisionObject.CompareTag("YourTag")){
// insert code here
}
PS: I never applied this to 2d games, so I don’t know if it works the same way.
yes but i want to add multible tags not only one
Multiple tags on the same objets?
Compare multiple tags?
Can you provide an example
multiple tags for multible objects
You can’t have more than one tag per objects.
Unless you create childs with each unique tags under the same parent.
Otherwise, I don’t know how to do what you want.
Sorry.
TRG96
August 23, 2014, 4:20pm
8
Do all the objects have scripts attached to them? if so then you can have
void OnTriggerEnter(Collider2D collider)
{
if(collider.gameObject.tag == "ground")
{
Destroy(gameObject);
}
}
Add this in the script of the collider on the objects you want to be destroyed and dont add this on the ones you dont want to be destroyed.
Lahzar
August 23, 2014, 7:22pm
9
I think this is something you want in your ontriggerenter2D:
if(collider.gameObject.tag == "tagToDestroy1" | collider.gameObject.tag == "tagToDestroy2" /*| etc.*/) {
//Destroy
} else {
print("do something else");
}
The | symbol means OR, so you dont need to repeat else if constantly!