Search Unity

Drag and Drop issue | gameobject is not being detected when Ontriggerenter2d

Discussion in 'Scripting' started by KUFgoddess, Feb 12, 2018.

  1. KUFgoddess

    KUFgoddess

    Joined:
    Apr 2, 2015
    Posts:
    27
    I cannot seem to understand why nothing is being detected I have 2d Colliders on both game objects and my player is set to a trigger. Why do you think its not displaying my Debug.Log("Detected"); when i drag a game object onto the player?



    Code (CSharp):
    1. public static  float distance = 1f;
    2.     private void OnMouseDrag()
    3.     {
    4.         Vector3 mousePosition = new Vector3(Input.mousePosition.x, Input.mousePosition.y,distance);
    5.         Vector3 objectPosition = Camera.main.ScreenToWorldPoint(mousePosition);
    6.         transform.position = objectPosition;
    7.  
    8. void OnTriggerEnter2D(Collider2D other)
    9.     {
    10.         if (other.tag == ("Food"))
    11.         {
    12.             Debug.Log("Detected");
    13.             RandomSFXGOOD();
    14.             anim.SetTrigger("Eating");
    15.             other.gameObject.SetActive(false);
    16.          
    17.         }
    18.         else
    19.         if (other.tag == ("Dangerous"))
    20.         {
    21.             anim.SetTrigger("Heaving");
    22.             RandomSFXBAD();
    23.             other.gameObject.SetActive(false);
    24.         }
    25.  
    26.     }
    //note i am trying to drag a 2d object onto another one and its not detecting the other I feel because the maincamera changes //the Z axis but even when i manually drag it nothing is detected.

    on my player i have attached this script and none of the game objects are being destroyed nor the logs or animation being triggered
     
  2. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    You can use: Mathf.Abs(transform.position.z - Camera.main.transform.position.z) for the 3rd parameter of mouse position.

    As for why your script is not working, are you sure there aren't any compiler errors? That script is improper. You did not close the method with a '}' brace - OnMouseDrag, I mean.

    For the OnTrigger to be called, at least 1 collider must be a trigger, and at least 1 of the colliding objects must have a rigidbody on it.

    Also ensure that your tags are spelled correctly, including capitalization. You can always 'Debug.Log' before the 'if' statements, to be sure.

    I have a couple suggestions for later:
    1) use "other.CompareTag("Food") instead. Small performance improvement, not functional difference.
    2) Consider learning and using the IPointer interfaces. IDragHandler for instance.
     
    KUFgoddess likes this.
  3. KUFgoddess

    KUFgoddess

    Joined:
    Apr 2, 2015
    Posts:
    27


    This is one of those moments where i forgot the rigidbody2D sorry! I am definitely going to start using Compare Tag.
    Those interfaces look quite handy I think I see how it would be set up Im reading it now and seeing how it would be extremely useful,thanks again.
     
  4. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    No problem, you're welcome.
     
    KUFgoddess likes this.