How to detect Collision on specific Object?











up vote
1
down vote

favorite












Let's suppose we have two cylinders in the scenes with red and blue materials on them. Also, we have two UI images with red and blue background. Now, what should I do to make the red image only draggable onto red cylinder and the blue image only draggable onto blue cylinder.

If I drag the red image onto the blue cylinder, then an error message should appear:

same for dragging blue image on red cylinder or vice versa.



See the picture below



Example



using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class wrench : MonoBehaviour {

public bool Dragging = false;
public bool collision = false;
Vector3 position;
public List<GameObject> UIimages;

public void BeginDrag() {

position = gameObject.transform.position;
Dragging = true;
foreach (GameObject obj in UIimages) {
obj.GetComponent<BoxCollider2D> ().enabled = false;
}
}

public void Drag() {
transform.position = Input.mousePosition;
}

public void Drop() {
if (!collision) {
gameObject.transform.position = position;
}
Dragging = false;
}
}









share|improve this question




















  • 1




    You can use colliders and tags to chek it, you know how they work?
    – Lotan
    Nov 20 at 10:07










  • @Lotan i know but i dont know that how to use them in this case because in future if i have so many objects in my scene i will have to write separate separate script for each of them
    – Nouman Khan
    Nov 20 at 10:11










  • @Lotan dragging is working very fine i just want that red can only be dragable on one red cylinder same for blue
    – Nouman Khan
    Nov 20 at 10:12










  • @Lotan please check my question i have posted the code now in this code i used the list which disables all the colliders on other objects but how to enable it back like if i am done with dragging red image on red cylinder then i want to drag blue image on blue cylinder but it cant detects the collision because its collider is disabled
    – Nouman Khan
    Nov 20 at 10:16










  • Sorry Nouman but I don't get your point, try to explain it different ^^'
    – Lotan
    Nov 20 at 13:54















up vote
1
down vote

favorite












Let's suppose we have two cylinders in the scenes with red and blue materials on them. Also, we have two UI images with red and blue background. Now, what should I do to make the red image only draggable onto red cylinder and the blue image only draggable onto blue cylinder.

If I drag the red image onto the blue cylinder, then an error message should appear:

same for dragging blue image on red cylinder or vice versa.



See the picture below



Example



using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class wrench : MonoBehaviour {

public bool Dragging = false;
public bool collision = false;
Vector3 position;
public List<GameObject> UIimages;

public void BeginDrag() {

position = gameObject.transform.position;
Dragging = true;
foreach (GameObject obj in UIimages) {
obj.GetComponent<BoxCollider2D> ().enabled = false;
}
}

public void Drag() {
transform.position = Input.mousePosition;
}

public void Drop() {
if (!collision) {
gameObject.transform.position = position;
}
Dragging = false;
}
}









share|improve this question




















  • 1




    You can use colliders and tags to chek it, you know how they work?
    – Lotan
    Nov 20 at 10:07










  • @Lotan i know but i dont know that how to use them in this case because in future if i have so many objects in my scene i will have to write separate separate script for each of them
    – Nouman Khan
    Nov 20 at 10:11










  • @Lotan dragging is working very fine i just want that red can only be dragable on one red cylinder same for blue
    – Nouman Khan
    Nov 20 at 10:12










  • @Lotan please check my question i have posted the code now in this code i used the list which disables all the colliders on other objects but how to enable it back like if i am done with dragging red image on red cylinder then i want to drag blue image on blue cylinder but it cant detects the collision because its collider is disabled
    – Nouman Khan
    Nov 20 at 10:16










  • Sorry Nouman but I don't get your point, try to explain it different ^^'
    – Lotan
    Nov 20 at 13:54













up vote
1
down vote

favorite









up vote
1
down vote

favorite











Let's suppose we have two cylinders in the scenes with red and blue materials on them. Also, we have two UI images with red and blue background. Now, what should I do to make the red image only draggable onto red cylinder and the blue image only draggable onto blue cylinder.

If I drag the red image onto the blue cylinder, then an error message should appear:

same for dragging blue image on red cylinder or vice versa.



See the picture below



Example



using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class wrench : MonoBehaviour {

public bool Dragging = false;
public bool collision = false;
Vector3 position;
public List<GameObject> UIimages;

public void BeginDrag() {

position = gameObject.transform.position;
Dragging = true;
foreach (GameObject obj in UIimages) {
obj.GetComponent<BoxCollider2D> ().enabled = false;
}
}

public void Drag() {
transform.position = Input.mousePosition;
}

public void Drop() {
if (!collision) {
gameObject.transform.position = position;
}
Dragging = false;
}
}









share|improve this question















Let's suppose we have two cylinders in the scenes with red and blue materials on them. Also, we have two UI images with red and blue background. Now, what should I do to make the red image only draggable onto red cylinder and the blue image only draggable onto blue cylinder.

If I drag the red image onto the blue cylinder, then an error message should appear:

same for dragging blue image on red cylinder or vice versa.



See the picture below



Example



using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class wrench : MonoBehaviour {

public bool Dragging = false;
public bool collision = false;
Vector3 position;
public List<GameObject> UIimages;

public void BeginDrag() {

position = gameObject.transform.position;
Dragging = true;
foreach (GameObject obj in UIimages) {
obj.GetComponent<BoxCollider2D> ().enabled = false;
}
}

public void Drag() {
transform.position = Input.mousePosition;
}

public void Drop() {
if (!collision) {
gameObject.transform.position = position;
}
Dragging = false;
}
}






c# user-interface unity3d






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 21 at 9:43









Nick Chapsas

2,3961314




2,3961314










asked Nov 20 at 9:14









Nouman Khan

56




56








  • 1




    You can use colliders and tags to chek it, you know how they work?
    – Lotan
    Nov 20 at 10:07










  • @Lotan i know but i dont know that how to use them in this case because in future if i have so many objects in my scene i will have to write separate separate script for each of them
    – Nouman Khan
    Nov 20 at 10:11










  • @Lotan dragging is working very fine i just want that red can only be dragable on one red cylinder same for blue
    – Nouman Khan
    Nov 20 at 10:12










  • @Lotan please check my question i have posted the code now in this code i used the list which disables all the colliders on other objects but how to enable it back like if i am done with dragging red image on red cylinder then i want to drag blue image on blue cylinder but it cant detects the collision because its collider is disabled
    – Nouman Khan
    Nov 20 at 10:16










  • Sorry Nouman but I don't get your point, try to explain it different ^^'
    – Lotan
    Nov 20 at 13:54














  • 1




    You can use colliders and tags to chek it, you know how they work?
    – Lotan
    Nov 20 at 10:07










  • @Lotan i know but i dont know that how to use them in this case because in future if i have so many objects in my scene i will have to write separate separate script for each of them
    – Nouman Khan
    Nov 20 at 10:11










  • @Lotan dragging is working very fine i just want that red can only be dragable on one red cylinder same for blue
    – Nouman Khan
    Nov 20 at 10:12










  • @Lotan please check my question i have posted the code now in this code i used the list which disables all the colliders on other objects but how to enable it back like if i am done with dragging red image on red cylinder then i want to drag blue image on blue cylinder but it cant detects the collision because its collider is disabled
    – Nouman Khan
    Nov 20 at 10:16










  • Sorry Nouman but I don't get your point, try to explain it different ^^'
    – Lotan
    Nov 20 at 13:54








1




1




You can use colliders and tags to chek it, you know how they work?
– Lotan
Nov 20 at 10:07




You can use colliders and tags to chek it, you know how they work?
– Lotan
Nov 20 at 10:07












@Lotan i know but i dont know that how to use them in this case because in future if i have so many objects in my scene i will have to write separate separate script for each of them
– Nouman Khan
Nov 20 at 10:11




@Lotan i know but i dont know that how to use them in this case because in future if i have so many objects in my scene i will have to write separate separate script for each of them
– Nouman Khan
Nov 20 at 10:11












@Lotan dragging is working very fine i just want that red can only be dragable on one red cylinder same for blue
– Nouman Khan
Nov 20 at 10:12




@Lotan dragging is working very fine i just want that red can only be dragable on one red cylinder same for blue
– Nouman Khan
Nov 20 at 10:12












@Lotan please check my question i have posted the code now in this code i used the list which disables all the colliders on other objects but how to enable it back like if i am done with dragging red image on red cylinder then i want to drag blue image on blue cylinder but it cant detects the collision because its collider is disabled
– Nouman Khan
Nov 20 at 10:16




@Lotan please check my question i have posted the code now in this code i used the list which disables all the colliders on other objects but how to enable it back like if i am done with dragging red image on red cylinder then i want to drag blue image on blue cylinder but it cant detects the collision because its collider is disabled
– Nouman Khan
Nov 20 at 10:16












Sorry Nouman but I don't get your point, try to explain it different ^^'
– Lotan
Nov 20 at 13:54




Sorry Nouman but I don't get your point, try to explain it different ^^'
– Lotan
Nov 20 at 13:54












1 Answer
1






active

oldest

votes

















up vote
0
down vote













Assuming your dropps are already detected as you say and the question is more about how to differ between the objects I would use a simple enum e.g.



public enum WhichObject
{
red,
blue
}


the advantage is that later you can very easily add more options without having to deal with layers, tags etc.



On the GameObject you drag around add a component e.g.



public class ObjectInformation
{
public WhichObject IsObject;
}


and simply select in the Inspector (or if you spawn them via script) for each of your dragged objects which value the WhichObject variable shall have.



Than on your target objects where you detect the collision also add a WhichObject variable but this time configure the value you expect to be dropped here



public WhichObject ExpectedObject;


again set it via the inspector or script.



Than simply add a check



var info = droppedObject.GetComponent<ObjectInformation>();

// Something else dropped?
if(!info) return;

// Now check if the value matches
// the one this object expects
if(info.IsObject == ExpectedObject)
{
// Correct object
Debug.Log("Yeah!");
}
else
{
// Wrong object
Debug.LogFormat("Dropped object {0} does not match the expected object {1}!", info.IsObject, ExpectedObject);
}




Later you can also simply extend it using multiple enums like e.g. colors, forms etc.



And maybe you could as well make the check the other way round and check on the dragged object, if the values match and if the don't match not even allow to be dropped on that target object (this depends of course on your implementation).






share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53389675%2fhow-to-detect-collision-on-specific-object%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    0
    down vote













    Assuming your dropps are already detected as you say and the question is more about how to differ between the objects I would use a simple enum e.g.



    public enum WhichObject
    {
    red,
    blue
    }


    the advantage is that later you can very easily add more options without having to deal with layers, tags etc.



    On the GameObject you drag around add a component e.g.



    public class ObjectInformation
    {
    public WhichObject IsObject;
    }


    and simply select in the Inspector (or if you spawn them via script) for each of your dragged objects which value the WhichObject variable shall have.



    Than on your target objects where you detect the collision also add a WhichObject variable but this time configure the value you expect to be dropped here



    public WhichObject ExpectedObject;


    again set it via the inspector or script.



    Than simply add a check



    var info = droppedObject.GetComponent<ObjectInformation>();

    // Something else dropped?
    if(!info) return;

    // Now check if the value matches
    // the one this object expects
    if(info.IsObject == ExpectedObject)
    {
    // Correct object
    Debug.Log("Yeah!");
    }
    else
    {
    // Wrong object
    Debug.LogFormat("Dropped object {0} does not match the expected object {1}!", info.IsObject, ExpectedObject);
    }




    Later you can also simply extend it using multiple enums like e.g. colors, forms etc.



    And maybe you could as well make the check the other way round and check on the dragged object, if the values match and if the don't match not even allow to be dropped on that target object (this depends of course on your implementation).






    share|improve this answer



























      up vote
      0
      down vote













      Assuming your dropps are already detected as you say and the question is more about how to differ between the objects I would use a simple enum e.g.



      public enum WhichObject
      {
      red,
      blue
      }


      the advantage is that later you can very easily add more options without having to deal with layers, tags etc.



      On the GameObject you drag around add a component e.g.



      public class ObjectInformation
      {
      public WhichObject IsObject;
      }


      and simply select in the Inspector (or if you spawn them via script) for each of your dragged objects which value the WhichObject variable shall have.



      Than on your target objects where you detect the collision also add a WhichObject variable but this time configure the value you expect to be dropped here



      public WhichObject ExpectedObject;


      again set it via the inspector or script.



      Than simply add a check



      var info = droppedObject.GetComponent<ObjectInformation>();

      // Something else dropped?
      if(!info) return;

      // Now check if the value matches
      // the one this object expects
      if(info.IsObject == ExpectedObject)
      {
      // Correct object
      Debug.Log("Yeah!");
      }
      else
      {
      // Wrong object
      Debug.LogFormat("Dropped object {0} does not match the expected object {1}!", info.IsObject, ExpectedObject);
      }




      Later you can also simply extend it using multiple enums like e.g. colors, forms etc.



      And maybe you could as well make the check the other way round and check on the dragged object, if the values match and if the don't match not even allow to be dropped on that target object (this depends of course on your implementation).






      share|improve this answer

























        up vote
        0
        down vote










        up vote
        0
        down vote









        Assuming your dropps are already detected as you say and the question is more about how to differ between the objects I would use a simple enum e.g.



        public enum WhichObject
        {
        red,
        blue
        }


        the advantage is that later you can very easily add more options without having to deal with layers, tags etc.



        On the GameObject you drag around add a component e.g.



        public class ObjectInformation
        {
        public WhichObject IsObject;
        }


        and simply select in the Inspector (or if you spawn them via script) for each of your dragged objects which value the WhichObject variable shall have.



        Than on your target objects where you detect the collision also add a WhichObject variable but this time configure the value you expect to be dropped here



        public WhichObject ExpectedObject;


        again set it via the inspector or script.



        Than simply add a check



        var info = droppedObject.GetComponent<ObjectInformation>();

        // Something else dropped?
        if(!info) return;

        // Now check if the value matches
        // the one this object expects
        if(info.IsObject == ExpectedObject)
        {
        // Correct object
        Debug.Log("Yeah!");
        }
        else
        {
        // Wrong object
        Debug.LogFormat("Dropped object {0} does not match the expected object {1}!", info.IsObject, ExpectedObject);
        }




        Later you can also simply extend it using multiple enums like e.g. colors, forms etc.



        And maybe you could as well make the check the other way round and check on the dragged object, if the values match and if the don't match not even allow to be dropped on that target object (this depends of course on your implementation).






        share|improve this answer














        Assuming your dropps are already detected as you say and the question is more about how to differ between the objects I would use a simple enum e.g.



        public enum WhichObject
        {
        red,
        blue
        }


        the advantage is that later you can very easily add more options without having to deal with layers, tags etc.



        On the GameObject you drag around add a component e.g.



        public class ObjectInformation
        {
        public WhichObject IsObject;
        }


        and simply select in the Inspector (or if you spawn them via script) for each of your dragged objects which value the WhichObject variable shall have.



        Than on your target objects where you detect the collision also add a WhichObject variable but this time configure the value you expect to be dropped here



        public WhichObject ExpectedObject;


        again set it via the inspector or script.



        Than simply add a check



        var info = droppedObject.GetComponent<ObjectInformation>();

        // Something else dropped?
        if(!info) return;

        // Now check if the value matches
        // the one this object expects
        if(info.IsObject == ExpectedObject)
        {
        // Correct object
        Debug.Log("Yeah!");
        }
        else
        {
        // Wrong object
        Debug.LogFormat("Dropped object {0} does not match the expected object {1}!", info.IsObject, ExpectedObject);
        }




        Later you can also simply extend it using multiple enums like e.g. colors, forms etc.



        And maybe you could as well make the check the other way round and check on the dragged object, if the values match and if the don't match not even allow to be dropped on that target object (this depends of course on your implementation).







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 21 at 6:42

























        answered Nov 21 at 6:37









        derHugo

        3,64521027




        3,64521027






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53389675%2fhow-to-detect-collision-on-specific-object%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            404 Error Contact Form 7 ajax form submitting

            How to know if a Active Directory user can login interactively

            TypeError: fit_transform() missing 1 required positional argument: 'X'