Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

[Help] Deleting same tag name in the Game

Discussion in 'Scripting' started by babji3, Sep 20, 2016.

  1. babji3

    babji3

    Joined:
    May 28, 2015
    Posts:
    179
    Hi guys, i need help in deleting gameobject with same tag.
    here is my scripts for swipeInputs.
    here i have Right Swipe and Left Swipt Inputs.
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class RightSwipeInputs : MonoBehaviour {
    5.  
    6.      Vector2 firstPressPos;
    7.      Vector2 secondPressPos;
    8.      Vector2 currentSwipe;
    9.  
    10.     void Update()
    11.     {
    12.         Swipe ();
    13.     }
    14.  
    15.     public void Swipe()
    16.     {
    17.         if(Input.GetMouseButtonDown(0))
    18.         {
    19.             //save began touch 2d point
    20.             firstPressPos = new Vector2(Input.mousePosition.x,Input.mousePosition.y);
    21.         }
    22.         if(Input.GetMouseButtonUp(0))
    23.         {
    24.             secondPressPos = new Vector2(Input.mousePosition.x,Input.mousePosition.y);
    25.  
    26.             currentSwipe = new Vector2(secondPressPos.x - firstPressPos.x, secondPressPos.y - firstPressPos.y);
    27.  
    28.             currentSwipe.Normalize();
    29.  
    30.             //_____________________swipe left______________________________//
    31.             if(currentSwipe.x < 0 &&  currentSwipe.y > -0.5f  && currentSwipe.y < 0.5f)
    32.             {
    33.                 RaycastHit2D hit = Physics2D.Raycast (new Vector2 (Camera.main.ScreenToWorldPoint (Input.mousePosition).x,0), Vector2.zero, 0);
    34.                 if (hit.collider.tag == "Right") {
    35.                     Destroy (gameObject);
    36.                     print (hit.collider.name);
    37.                 }
    38.             }
    39.             //_____________________swipe right______________________________//
    40.             if(currentSwipe.x > 0  && currentSwipe.y > -0.5f  && currentSwipe.y < 0.5f)
    41.             {
    42.                 RaycastHit2D hit = Physics2D.Raycast (new Vector2 (Camera.main.ScreenToWorldPoint (Input.mousePosition).x, 0), Vector2.zero, 0);
    43.                 if (hit.collider.tag == "Right") {
    44.                    
    45.                     print ("CORRECT");
    46.                 }
    47.             }
    48.  
    49.         }
    50.     }
    51. }
    52.  
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class LeftSwipeInputs : MonoBehaviour {
    5.  
    6.     Vector2 firstPressPos;
    7.     Vector2 secondPressPos;
    8.     Vector2 currentSwipe;
    9.  
    10.     void Update()
    11.     {
    12.         Swipe ();
    13.     }
    14.  
    15.     public void Swipe()
    16.     {
    17.         if(Input.GetMouseButtonDown(0))
    18.         {
    19.             firstPressPos = new Vector2(Input.mousePosition.x,Input.mousePosition.y);
    20.         }
    21.         if(Input.GetMouseButtonUp(0))
    22.         {
    23.             secondPressPos = new Vector2(Input.mousePosition.x,Input.mousePosition.y);
    24.  
    25.             currentSwipe = new Vector2(secondPressPos.x - firstPressPos.x, secondPressPos.y - firstPressPos.y);
    26.  
    27.             currentSwipe.Normalize();
    28.  
    29.  
    30.             //_____________________swipe right______________________________//
    31.             if(currentSwipe.x > 0  && currentSwipe.y > -0.5f  && currentSwipe.y < 0.5f)
    32.             {
    33.                 RaycastHit2D hit = Physics2D.Raycast (new Vector2 (Camera.main.ScreenToWorldPoint (Input.mousePosition).x, 0), Vector2.zero, 0);
    34.                 if (hit.collider.tag == "Left") {
    35.                     Destroy (gameObject);
    36.                     print (hit.collider.name);
    37.                 }
    38.             }
    39.             //_____________________swipe left______________________________//
    40.             if(currentSwipe.x < 0 &&  currentSwipe.y > -0.5f  && currentSwipe.y < 0.5f)
    41.             {
    42.                 RaycastHit2D hit = Physics2D.Raycast (new Vector2 (Camera.main.ScreenToWorldPoint (Input.mousePosition).x,0), Vector2.zero, 0);
    43.                 if (hit.collider.tag == "Left") {
    44.                    
    45.                     print ("CORRECT");
    46.                 }
    47.             }
    48.         }
    49.     }
    50. }
    51.  
    When i swipe the right hand side object with rightswipe it should correct and when i swipe right hand object with left swipt it should wrong, for now it is good i achive this, the concept is you should not swipe wrong objects.
    here you can see the Screenshots ..
    SS1.jpg SS2.jpg SS3.jpg SS4.jpg

    But when i dublicate this objects the same tag name object is deleting, but i dont want this.

    Plz give me some good idea in this....
     
  2. IsGreen

    IsGreen

    Joined:
    Jan 17, 2014
    Posts:
    206
    Destroy by name.
     
  3. babji3

    babji3

    Joined:
    May 28, 2015
    Posts:
    179
    i try that one but the same issue.
     
  4. Laperen

    Laperen

    Joined:
    Feb 1, 2016
    Posts:
    1,065
    Your screenshots do not provide any information. you will need to explain how your game is played and how it works before attempting to explain what those screenshots show.
     
  5. babji3

    babji3

    Joined:
    May 28, 2015
    Posts:
    179
    1st screenshot is for correct swipe it is working, and in this no one object will destroy

    2st screenshot is for wrong swipe on different direction(for example for right object i will swipe left it will destroy)

    3rd screenshot is showing more than one object with the same name/tag

    4rth screenshot is showing if i do wrong swipe the same tag objects is destroying.
     
  6. IsGreen

    IsGreen

    Joined:
    Jan 17, 2014
    Posts:
    206
    Because you use same name to all clones. Change gameobject name when you instantiate it.
     
  7. babji3

    babji3

    Joined:
    May 28, 2015
    Posts:
    179
    Like??
    Any sample code u have??
     
  8. babji3

    babji3

    Joined:
    May 28, 2015
    Posts:
    179
    Plz can any one have good suggestion ???
     
  9. Laperen

    Laperen

    Joined:
    Feb 1, 2016
    Posts:
    1,065
    I don't even understand why you need 2 scripts to detect swipe input in the first place, let alone know what your problem is. And before I can even get to understand your problem, I need to know what this thing is going to be used for before a step-by-step description can make sense. Right now your question is just badly explained.

    I'd rather you explain your game in terms of how it plays and what is supposed to happen. No scripts, just concepts, because to me I feel the way you are doing things is already fundamentally unstable. If you build on it, it's just going to topple sooner than later.
     
    Kiwasi likes this.
  10. babji3

    babji3

    Joined:
    May 28, 2015
    Posts:
    179
    Hi Laperen

    My concept is the objects are instantiate on the screen right and left, these objects is tagged as left or right randomly,

    If you swipe right and you would touch/click on right tagged/named object you will get point otherwise if you swipe wrong direction like for right tagged object if you swipe left you lose.

    This is the thing.
     
  11. IsGreen

    IsGreen

    Joined:
    Jan 17, 2014
    Posts:
    206
    Try: Destroy(GameObject.Find(hit.transform.name);
    You need to apply different names to GameObject, for example, "Right"+Time.time.ToString().
     
  12. babji3

    babji3

    Joined:
    May 28, 2015
    Posts:
    179
    ok thx i will try this one..let me tell you.
     
  13. babji3

    babji3

    Joined:
    May 28, 2015
    Posts:
    179
    can you plz tell me the difference between touch and mouse clicks?? like

    touch begin
    OnMouseDown(0)

    touch end
    OnMouseUp(0)

    touch moved
    ?????????
     
  14. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    Forget naming. Just hold onto the references when you Instantiate the object.
     
  15. babji3

    babji3

    Joined:
    May 28, 2015
    Posts:
    179
    hy i solve that bug, its just simple line and it work fine now as i though
    here is the code
    Code (CSharp):
    1. //_____________________swipe left______________________________//
    2.             if(currentSwipe.x < 0 &&  currentSwipe.y > -0.5f  && currentSwipe.y < 0.5f)
    3.             {
    4.                 RaycastHit2D hitInfo = Physics2D.Raycast (new Vector2 (Camera.main.ScreenToWorldPoint (Input.mousePosition).x, Camera.main.ScreenToWorldPoint (Input.mousePosition).y), Vector2.zero, 0);
    5.                 if (hitInfo) {
    6.                     if (hitInfo.collider!=null && hitInfo.collider.tag != "Left") {
    7.                         Destroy (hitInfo.transform.gameObject);
    8.                         print ("WRONG");
    9.                     }
    10.                 }
    11.             }
    12.             //_____________________swipe right______________________________//
    13.             if(currentSwipe.x > 0  && currentSwipe.y > -0.5f  && currentSwipe.y < 0.5f)
    14.             {
    15.                 RaycastHit2D hitInfo = Physics2D.Raycast (new Vector2 (Camera.main.ScreenToWorldPoint (Input.mousePosition).x, Camera.main.ScreenToWorldPoint (Input.mousePosition).y), Vector2.zero, 0);
    16.                 if (hitInfo) {
    17.                     if (hitInfo.collider.tag == "Right") {
    18.                         Destroy (hitInfo.transform.gameObject);
    19.                         print ("CORRECT");
    20.                         GameManager.instance.count++;
    21.                     }
    22.                 }
    hope it helps for someone.