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
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
|
show 2 more comments
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
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
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
|
show 2 more comments
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
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
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
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
c# user-interface unity3d
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
|
show 2 more comments
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
|
show 2 more comments
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).
add a comment |
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).
add a comment |
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).
add a comment |
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).
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).
edited Nov 21 at 6:42
answered Nov 21 at 6:37
derHugo
3,64521027
3,64521027
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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