Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

how can i put tags for my code ?

Discussion in 'Scripting' started by karammaks, Aug 23, 2014.

  1. karammaks

    karammaks

    Joined:
    Apr 6, 2014
    Posts:
    146
    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 ?

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class DestroyGround : MonoBehaviour {
    5.  
    6.  
    7.     void OnTriggerEnter2D(Collider2D collisionObject){
    8.            
    9.        
    10.         Destroy (collisionObject.gameObject);
    11.        
    12.  
    13.        
    14.        
    15.     }
    16. }
     
  2. karammaks

    karammaks

    Joined:
    Apr 6, 2014
    Posts:
    146
    note : they are multible objects i want to tag
     
  3. laurelhach

    laurelhach

    Joined:
    Dec 1, 2013
    Posts:
    229
    Is it what you are asking for ?
    http://docs.unity3d.com/Manual/Tags.html

    and to check a tag :
    Code (csharp):
    1.  
    2. if(collisionObject.CompareTag("YourTag")){
    3. // insert code here
    4. }
    5.  
    PS: I never applied this to 2d games, so I don't know if it works the same way.
     
  4. karammaks

    karammaks

    Joined:
    Apr 6, 2014
    Posts:
    146
    yes but i want to add multible tags not only one
     
  5. laurelhach

    laurelhach

    Joined:
    Dec 1, 2013
    Posts:
    229
    Multiple tags on the same objets?
    Compare multiple tags?

    Can you provide an example
     
  6. karammaks

    karammaks

    Joined:
    Apr 6, 2014
    Posts:
    146
    multiple tags for multible objects :)
     
  7. laurelhach

    laurelhach

    Joined:
    Dec 1, 2013
    Posts:
    229
    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.
     
  8. TRG96

    TRG96

    Joined:
    Mar 26, 2011
    Posts:
    102
    Do all the objects have scripts attached to them? if so then you can have
    Code (CSharp):
    1. void OnTriggerEnter(Collider2D collider)
    2. {
    3. if(collider.gameObject.tag == "ground")
    4. {
    5. Destroy(gameObject);
    6. }
    7. }
    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.
     
  9. Lahzar

    Lahzar

    Joined:
    May 6, 2013
    Posts:
    87
    I think this is something you want in your ontriggerenter2D:

    Code (CSharp):
    1.  
    2. if(collider.gameObject.tag == "tagToDestroy1" | collider.gameObject.tag == "tagToDestroy2" /*| etc.*/) {
    3.    //Destroy
    4. } else {
    5.    print("do something else");
    6. }
    7.  
    The | symbol means OR, so you dont need to repeat else if constantly!